@Test public void testWithResponsiveMessageDelegateNoDefaultDestination_SendsReturnTextMessageWhenSessionSupplied() throws Exception { Queue destination = mock(Queue.class); TextMessage sentTextMessage = mock(TextMessage.class); // correlation ID is queried when response is being created... given(sentTextMessage.getJMSCorrelationID()).willReturn(null); given(sentTextMessage.getJMSMessageID()).willReturn(CORRELATION_ID); // Reply-To is queried when response is being created... given(sentTextMessage.getJMSReplyTo()).willReturn(destination); TextMessage responseTextMessage = mock(TextMessage.class); MessageProducer messageProducer = mock(MessageProducer.class); Session session = mock(Session.class); given(session.createTextMessage(RESPONSE_TEXT)).willReturn(responseTextMessage); given(session.createProducer(destination)).willReturn(messageProducer); ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class); given(delegate.handleMessage(sentTextMessage)).willReturn(RESPONSE_TEXT); MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) { @Override protected Object extractMessage(Message message) { return message; } }; adapter.onMessage(sentTextMessage, session); verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID); verify(messageProducer).send(responseTextMessage); verify(messageProducer).close(); verify(delegate).handleMessage(sentTextMessage); }
given(sentTextMessage.getJMSReplyTo()).willReturn(destination);
@Test public void testWithResponsiveMessageDelegateWithDefaultDestination_SendsReturnTextMessageWhenSessionSupplied() throws Exception { Queue destination = mock(Queue.class); TextMessage sentTextMessage = mock(TextMessage.class); // correlation ID is queried when response is being created... given(sentTextMessage.getJMSCorrelationID()).willReturn( CORRELATION_ID); // Reply-To is queried when response is being created... given(sentTextMessage.getJMSReplyTo()).willReturn(null); // we want to fall back to the default... TextMessage responseTextMessage = mock(TextMessage.class); QueueSender queueSender = mock(QueueSender.class); Session session = mock(Session.class); given(session.createTextMessage(RESPONSE_TEXT)).willReturn(responseTextMessage); given(session.createProducer(destination)).willReturn(queueSender); ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class); given(delegate.handleMessage(sentTextMessage)).willReturn(RESPONSE_TEXT); MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) { @Override protected Object extractMessage(Message message) { return message; } }; adapter.setDefaultResponseDestination(destination); adapter.onMessage(sentTextMessage, session); verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID); verify(queueSender).send(responseTextMessage); verify(queueSender).close(); verify(delegate).handleMessage(sentTextMessage); }
@Test public void testWithResponsiveMessageDelegateNoDefaultDestinationAndNoReplyToDestination_SendsReturnTextMessageWhenSessionSupplied() throws Exception { final TextMessage sentTextMessage = mock(TextMessage.class); // correlation ID is queried when response is being created... given(sentTextMessage.getJMSCorrelationID()).willReturn(CORRELATION_ID); // Reply-To is queried when response is being created... given(sentTextMessage.getJMSReplyTo()).willReturn(null); TextMessage responseTextMessage = mock(TextMessage.class); final QueueSession session = mock(QueueSession.class); given(session.createTextMessage(RESPONSE_TEXT)).willReturn(responseTextMessage); ResponsiveMessageDelegate delegate = mock(ResponsiveMessageDelegate.class); given(delegate.handleMessage(sentTextMessage)).willReturn(RESPONSE_TEXT); final MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) { @Override protected Object extractMessage(Message message) { return message; } }; try { adapter.onMessage(sentTextMessage, session); fail("expected CouldNotSendReplyException with InvalidDestinationException"); } catch (ReplyFailureException ex) { assertEquals(InvalidDestinationException.class, ex.getCause().getClass()); } verify(responseTextMessage).setJMSCorrelationID(CORRELATION_ID); verify(delegate).handleMessage(sentTextMessage); }
@Test public void testWithResponsiveMessageDelegateWhenReturnTypeIsAJMSMessageAndNoMessageConverterIsSupplied() throws Exception { Queue destination = mock(Queue.class); final TextMessage sentTextMessage = mock(TextMessage.class); // correlation ID is queried when response is being created... given(sentTextMessage.getJMSCorrelationID()).willReturn(CORRELATION_ID); // Reply-To is queried when response is being created... given(sentTextMessage.getJMSReplyTo()).willReturn(destination); TextMessage responseMessage = mock(TextMessage.class); QueueSender queueSender = mock(QueueSender.class); Session session = mock(Session.class); given(session.createProducer(destination)).willReturn(queueSender); ResponsiveJmsTextMessageReturningMessageDelegate delegate = mock(ResponsiveJmsTextMessageReturningMessageDelegate.class); given(delegate.handleMessage(sentTextMessage)).willReturn(responseMessage); final MessageListenerAdapter adapter = new MessageListenerAdapter(delegate) { @Override protected Object extractMessage(Message message) { return message; } }; adapter.setMessageConverter(null); adapter.onMessage(sentTextMessage, session); verify(responseMessage).setJMSCorrelationID(CORRELATION_ID); verify(queueSender).send(responseMessage); verify(queueSender).close(); }
private void handleRequest(Session session, MessageConsumer consumer, MessageProducer producer) throws JMSException { TextMessage msg = (TextMessage) consumer.receive(1000); log.info("Got request: " + msg.getText()); Destination replyDest = msg.getJMSReplyTo(); String correlationId = msg.getJMSCorrelationID(); msg = session.createTextMessage("Pong"); msg.setJMSDestination(replyDest); msg.setJMSCorrelationID(correlationId); producer.send(msg); }
public static void main(String[] args) throws Exception { try { // The configuration for the Qpid InitialContextFactory has been supplied in // a jndi.properties file in the classpath, which results in it being picked // up automatically by the InitialContext constructor. Context context = new InitialContext(); ConnectionFactory factory = (ConnectionFactory) context.lookup("myFactoryLookup"); Destination queue = (Destination) context.lookup("myQueueLookup"); Connection connection = factory.createConnection(System.getProperty("USER"), System.getProperty("PASSWORD")); connection.setExceptionListener(new MyExceptionListener()); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer messageConsumer = session.createConsumer(queue); MessageProducer messageProducer = session.createProducer(null); while (true) { //Receive messages and return a new uppercase message. TextMessage requestMessage = (TextMessage) messageConsumer.receive(); System.out.println("[SERVER] Received: " + requestMessage.getText()); TextMessage responseMessage = session.createTextMessage(requestMessage.getText().toUpperCase()); messageProducer.send(requestMessage.getJMSReplyTo(), responseMessage, DeliveryMode.NON_PERSISTENT, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); } } catch (Exception exp) { System.out.println("[SERVER] Caught exception, exiting."); exp.printStackTrace(System.out); System.exit(1); } }
public void onMessage(Message message){ try { TextMessage textMessage = (TextMessage)message; Queue replyQueue = (Queue)textMessage.getJMSReplyTo(); Message replyMessage = session.createMessage(); replyMessage.setJMSCorrelationID(message.getJMSMessageID()); sender.send(replyQueue, replyMessage); } catch (JMSException jmse){jmse.printStackTrace();} }
/** * Use the asynchronous subscription mechanism */ @Override public void onMessage(Message message) { try { TextMessage requestMessage = (TextMessage) message; System.out.println("Received request from " + requestDestination); System.out.println(requestMessage.toString()); Destination replyDestination = requestMessage.getJMSReplyTo(); // TODO // String value = // ActiveMQDestination.getClientId((ActiveMQDestination) // replyDestination); // assertEquals("clientID from the temporary destination must be the // same", clientSideClientID, value); TextMessage replyMessage = serverSession.createTextMessage("Hello: " + requestMessage.getText()); replyMessage.setJMSCorrelationID(requestMessage.getJMSMessageID()); if (dynamicallyCreateProducer) { replyProducer = serverSession.createProducer(replyDestination); replyProducer.send(replyMessage); } else { replyProducer.send(replyDestination, replyMessage); } System.out.println("Sent reply to " + replyDestination); System.out.println(replyMessage.toString()); } catch (JMSException e) { onException(e); } }
@Override public JmsRecord mapMessage(Message message) throws Exception { TextMessage textMessage = (TextMessage) message; Map<String, Object> properties = new HashMap<>(); @SuppressWarnings("rawtypes") Enumeration propertyNames = textMessage.getPropertyNames(); while (propertyNames.hasMoreElements()) { String propertyName = (String) propertyNames.nextElement(); properties.put(propertyName, textMessage.getObjectProperty(propertyName)); } JmsRecord jmsRecord = new JmsRecord( textMessage.getJMSMessageID(), textMessage.getJMSTimestamp(), textMessage.getJMSCorrelationID(), textMessage.getJMSReplyTo(), textMessage.getJMSDestination(), textMessage.getJMSDeliveryMode(), textMessage.getJMSRedelivered(), textMessage.getJMSType(), textMessage.getJMSExpiration(), textMessage.getJMSPriority(), properties, textMessage.getText()); return jmsRecord; } })
@Test(timeout = 60000) public void testReplyToUsingTempQueue() throws Throwable { Connection connection = createConnection(); try { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); TemporaryQueue queue = session.createTemporaryQueue(); MessageProducer p = session.createProducer(queue); TextMessage message = session.createTextMessage(); message.setText("Message temporary"); message.setJMSReplyTo(session.createTemporaryQueue()); p.send(message); MessageConsumer cons = session.createConsumer(queue); connection.start(); message = (TextMessage) cons.receive(5000); Destination jmsReplyTo = message.getJMSReplyTo(); assertNotNull(jmsReplyTo); assertNotNull(message); } finally { connection.close(); } }
@Test(timeout = 60000) public void testReplyToUsingQueue() throws Throwable { Connection connection = createConnection(); try { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); TemporaryQueue queue = session.createTemporaryQueue(); MessageProducer p = session.createProducer(queue); TextMessage message = session.createTextMessage(); message.setText("Message temporary"); message.setJMSReplyTo(session.createQueue(getQueueName())); p.send(message); MessageConsumer cons = session.createConsumer(queue); connection.start(); message = (TextMessage) cons.receive(5000); assertNotNull(message); Destination jmsReplyTo = message.getJMSReplyTo(); assertNotNull(jmsReplyTo); assertNotNull(message); } finally { connection.close(); } }
final DaemonRequest request = new DaemonRequest(textMessage.getJMSCorrelationID(), textMessage.getJMSReplyTo(), ivoIn); if (icoClass.equals(PingIVO.class)) { this.reply(new DaemonResponse(request, new PongIVO.PongIVOBuilder().build()), secure);
System.out.println(received.getText()); System.out.println("check temp queue on server #2"); MessageProducer tempProducer = session2.createProducer(received.getJMSReplyTo()); tempProducer.send(session2.createTextMessage(">>> " + received.getText())); tempProducer.close();
System.out.println(received.getText()); System.out.println("check temp queue on server #2"); MessageProducer tempProducer = session2.createProducer(received.getJMSReplyTo()); tempProducer.send(session2.createTextMessage(">>> " + received.getText())); tempProducer.close();
responseMessage.setText(payload); responseMessage.setJMSCorrelationID(textMessage.getJMSCorrelationID()); replyProducer.send(textMessage.getJMSReplyTo(), responseMessage); } else { if (incomingMessage instanceof Notification) {
responseMessage.setText(payload); responseMessage.setJMSCorrelationID(textMessage.getJMSCorrelationID()); replyProducer.send(textMessage.getJMSReplyTo(), responseMessage); } else { if (incomingMessage instanceof Notification) {