Java Code Examples for javax.jms.TextMessage#setJMSCorrelationID()
The following examples show how to use
javax.jms.TextMessage#setJMSCorrelationID() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestReplyExample.java From activemq-artemis with Apache License 2.0 | 6 votes |
@Override public void onMessage(final Message request) { try { System.out.println("Received request message: " + ((TextMessage) request).getText()); // Extract the ReplyTo destination Destination replyDestination = request.getJMSReplyTo(); System.out.println("Reply to queue: " + replyDestination); // Create the reply message TextMessage replyMessage = session.createTextMessage("A reply message"); // Set the CorrelationID, using message id. replyMessage.setJMSCorrelationID(request.getJMSMessageID()); // Send out the reply message replyProducer.send(replyDestination, replyMessage); System.out.println("Reply sent"); } catch (JMSException e) { e.printStackTrace(); } }
Example 2
Source File: PooledConnectionTempQueueTest.java From cxf with Apache License 2.0 | 6 votes |
public static void receiveAndRespondWithMessageIdAsCorrelationId(ConnectionFactory connectionFactory, String queueName) throws JMSException { Connection con = connectionFactory.createConnection(); Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(session.createQueue(queueName)); final javax.jms.Message inMessage = consumer.receive(); //String requestMessageId = inMessage.getJMSMessageID(); //System.out.println("Received message " + requestMessageId); final TextMessage replyMessage = session.createTextMessage("Result"); replyMessage.setJMSCorrelationID(inMessage.getJMSMessageID()); final MessageProducer producer = session.createProducer(inMessage.getJMSReplyTo()); //System.out.println("Sending reply to " + inMessage.getJMSReplyTo()); producer.send(replyMessage); producer.close(); consumer.close(); session.close(); con.close(); }
Example 3
Source File: AMQPClient.java From amazon-mq-workshop with Apache License 2.0 | 6 votes |
private static void sendMessages(Session session, MessageProducer messageProducer, String name, int interval, int deliveryMode, WrapInt count) throws JMSException { String destination = messageProducer.getDestination().toString(); while (true) { count.v++; String id = UUID.randomUUID().toString(); TextMessage message = session.createTextMessage(String.format("[%s] [%s] Message number %s", destination, name, count.v)); message.setJMSMessageID(id); message.setJMSCorrelationID(id); messageProducer.send(message, deliveryMode, 0, -1); if (interval > 0) { System.out.println(String.format("%s - Sender: sent '%s'", df.format(new Date()), message.getText())); try { Thread.sleep(interval); } catch (InterruptedException e) { e.printStackTrace(); } } } }
Example 4
Source File: PooledConnectionTempQueueTest.java From pooled-jms with Apache License 2.0 | 6 votes |
public void receiveAndRespondWithMessageIdAsCorrelationId(ConnectionFactory connectionFactory, String queueName) throws JMSException { Connection connection = connectionFactory.createConnection(); try { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(session.createQueue(queueName)); final javax.jms.Message inMessage = consumer.receive(); String requestMessageId = inMessage.getJMSMessageID(); LOG.debug("Received message " + requestMessageId); final TextMessage replyMessage = session.createTextMessage("Result"); replyMessage.setJMSCorrelationID(inMessage.getJMSMessageID()); final MessageProducer producer = session.createProducer(inMessage.getJMSReplyTo()); LOG.debug("Sending reply to " + inMessage.getJMSReplyTo()); producer.send(replyMessage); producer.close(); consumer.close(); session.close(); } finally { connection.close(); } }
Example 5
Source File: AmazonMqClient.java From amazon-mq-workshop with Apache License 2.0 | 6 votes |
private static void sendMessages(Session session, MessageProducer queueMessageProducer, long ttl, String name, int interval, int deliveryMode, WrapInt count) throws JMSException { String destination = queueMessageProducer.getDestination().toString(); while (true) { count.v++; String id = UUID.randomUUID().toString(); TextMessage message = session.createTextMessage(String.format("[%s] [%s] Message number %s", destination, name, count.v)); message.setJMSMessageID(id); message.setJMSCorrelationID(id); queueMessageProducer.send(message, deliveryMode, 0, ttl); if (interval > 0) { System.out.println(String.format("%s - Sender: sent '%s'", df.format(new Date()), message.getText())); try { Thread.sleep(interval); } catch (InterruptedException e) { e.printStackTrace(); } } } }
Example 6
Source File: InductorPublisher.java From oneops with Apache License 2.0 | 5 votes |
/** * Publish message. * * @param waitTaskName the wait task name * @param woType the wo type * @throws JMSException the jMS exception */ public void publishMessage(String processId, String execId, CmsWorkOrderSimpleBase wo, String waitTaskName, String woType) throws JMSException { SimpleDateFormat format = new SimpleDateFormat(CmsConstants.SEARCH_TS_PATTERN); wo.getSearchTags().put(CmsConstants.REQUEST_ENQUE_TS, format.format(new Date())); //guarantee non empty-value for searchMap if (version != null && StringUtils.isNotBlank(version.getGitVersion())) { wo.getSearchTags().put(CONTROLLLER_VERSION_SEARCH_TAG, version.getGitVersion()); } else { wo.getSearchTags().put(CONTROLLLER_VERSION_SEARCH_TAG, DEFAULT_VERSION); } TextMessage message = session.createTextMessage(gson.toJson(wo)); String corelationId = processId + "!" + execId + "!" + waitTaskName+"!"+getCtxtId(wo); message.setJMSCorrelationID(corelationId); message.setStringProperty("task_id", corelationId); message.setStringProperty("type", woType); String queueName = getQueue(wo); bindingQueusMap.computeIfAbsent(queueName, k -> { try { return newMessageProducer(k); } catch (JMSException e) { throw new RuntimeException(e); } }).send(message); if (logger.isDebugEnabled()) { logger.debug("Published: " + message.getText()); } logger.info("Posted message with id "+ corelationId +" to q: "+queueName); }
Example 7
Source File: MessageProducer.java From ditto-examples with Eclipse Public License 2.0 | 5 votes |
private void setResponse(final Session session, final TextMessage message, final String correlationId) throws JMSException { final Destination responseDestination = jmsTemplate.getDestinationResolver().resolveDestinationName(session, response, true); message.setJMSCorrelationID(correlationId); message.setJMSReplyTo(responseDestination); }
Example 8
Source File: AmqpClientActorTest.java From ditto with Eclipse Public License 2.0 | 5 votes |
private static Message mockMessage() throws JMSException { final AmqpJmsTextMessageFacade amqpJmsTextMessageFacade = new AmqpJmsTextMessageFacade(); amqpJmsTextMessageFacade.setContentType(Symbol.getSymbol(DittoConstants.DITTO_PROTOCOL_CONTENT_TYPE)); amqpJmsTextMessageFacade.initialize(Mockito.mock(AmqpConnection.class)); final TextMessage jmsTextMessage = new JmsTextMessage(amqpJmsTextMessageFacade); jmsTextMessage.setJMSCorrelationID("cid"); jmsTextMessage.setJMSReplyTo(new JmsQueue("reply")); jmsTextMessage.setText(TestConstants.modifyThing()); return jmsTextMessage; }
Example 9
Source File: JmsTopicRequestReplyTest.java From activemq-artemis with Apache License 2.0 | 5 votes |
/** * Use the asynchronous subscription mechanism */ @Override public void onMessage(Message message) { try { TextMessage requestMessage = (TextMessage) message; 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); } } catch (JMSException e) { onException(e); } }
Example 10
Source File: TemporaryJMSQueueClusterTest.java From activemq-artemis with Apache License 2.0 | 5 votes |
@Override public void onMessage(final javax.jms.Message request) { try { Destination replyDestination = request.getJMSReplyTo(); TextMessage replyMessage = session.createTextMessage("A reply message"); replyMessage.setJMSCorrelationID(request.getJMSMessageID()); replyProducer.send(replyDestination, replyMessage); } catch (JMSException e) { e.printStackTrace(); } }
Example 11
Source File: JmsTopicRequestReplyTest.java From activemq-artemis with Apache License 2.0 | 5 votes |
/** * Use the asynchronous subscription mechanism */ @Override public void onMessage(Message message) { try { TextMessage requestMessage = (TextMessage) message; LOG.info("Received request."); LOG.info(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); } LOG.info("Sent reply."); LOG.info(replyMessage.toString()); } catch (JMSException e) { onException(e); } }
Example 12
Source File: BaseTest.java From a with Apache License 2.0 | 5 votes |
private TextMessage createTextMessage(String testCorrId, String stringPropertyValue, String utfText, Queue replyQueue) throws JMSException { final TextMessage tm1 = session.createTextMessage(utfText); tm1.setStringProperty("myStringProperty", stringPropertyValue); tm1.setIntProperty("myIntProperty", 42); tm1.setDoubleProperty("myDoubleProperty", Math.PI); tm1.setJMSType("myJmsType"); tm1.setJMSCorrelationID(testCorrId); tm1.setJMSDeliveryMode(DeliveryMode.PERSISTENT); tm1.setJMSPriority(2); tm1.setJMSReplyTo(replyQueue); return tm1; }
Example 13
Source File: TestReceiver.java From cxf with Apache License 2.0 | 5 votes |
private void receiveAndRespond() { try (ResourceCloser closer = new ResourceCloser()) { Connection connection = closer.register(connectionFactory.createConnection()); connection.start(); Session session = closer.register(connection.createSession(false, Session.AUTO_ACKNOWLEDGE)); MessageConsumer consumer = closer.register(session.createConsumer(session .createQueue(receiveQueueName))); final javax.jms.Message inMessage = consumer.receive(10000); if (inMessage == null) { //System.out.println("TestReceiver timed out"); throw new RuntimeException("No message received on destination " + receiveQueueName); } requestMessageId = inMessage.getJMSMessageID(); //System.out.println("Received message " + requestMessageId); final TextMessage replyMessage = session.createTextMessage("Result"); String correlationId = (forceMessageIdAsCorrelationId || inMessage.getJMSCorrelationID() == null) ? inMessage.getJMSMessageID() : inMessage.getJMSCorrelationID(); replyMessage.setJMSCorrelationID(correlationId); Destination replyDest = staticReplyQueue != null ? session.createQueue(staticReplyQueue) : inMessage.getJMSReplyTo(); if (replyDest != null) { final MessageProducer producer = closer .register(session.createProducer(replyDest)); //System.out.println("Sending reply with correlation id " + correlationId + " to " + replyDest); producer.send(replyMessage); } } catch (Throwable e) { ex = e; } }
Example 14
Source File: QueueConsumerTest.java From ballerina-message-broker with Apache License 2.0 | 4 votes |
@Parameters({ "broker-port", "admin-username", "admin-password", "broker-hostname" }) @Test public void testConsumerWithBasicRecover(String port, String adminUsername, String adminPassword, String brokerHostname) throws Exception { String queueName = "testConsumerWithBasicRecover"; InitialContext initialContextForQueue = ClientHelper .getInitialContextBuilder(adminUsername, adminPassword, brokerHostname, port) .withQueue(queueName) .build(); ConnectionFactory connectionFactory = (ConnectionFactory) initialContextForQueue.lookup(ClientHelper.CONNECTION_FACTORY); Connection connection = connectionFactory.createConnection(); connection.start(); // publish message Session producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = producerSession.createQueue(queueName); MessageProducer producer = producerSession.createProducer(queue); TextMessage firstMessage = producerSession.createTextMessage("First message for reject test"); TextMessage secondMessage = producerSession.createTextMessage("Second message for reject test"); String correlationIdOne = "1"; String correlationIdTwo = "2"; firstMessage.setJMSCorrelationID(correlationIdOne); secondMessage.setJMSCorrelationID(correlationIdTwo); producer.send(firstMessage); producer.send(secondMessage); producerSession.close(); // Consume published messages Session subscriberSession = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); Destination subscriberDestination = (Destination) initialContextForQueue.lookup(queueName); MessageConsumer consumer = subscriberSession.createConsumer(subscriberDestination); Message message = consumer.receive(5000); Assert.assertNotNull(message, "Message was not received"); subscriberSession.recover(); // Message order can change after recovering for (int i = 0; i < 2; i++) { message = consumer.receive(5000); Assert.assertNotNull(message, "Requeued message was not received"); if (correlationIdOne.equals(message.getJMSCorrelationID())) { Assert.assertTrue(message.getJMSRedelivered(), "Redelivered flag was set in second message" + message); } else { Assert.assertFalse(message.getJMSRedelivered(), "Redelivered flag was set in first message"); } message.acknowledge(); } connection.close(); }
Example 15
Source File: BasicRequestor.java From solace-samples-jms with Apache License 2.0 | 4 votes |
public void run(String... args) throws Exception { String[] split = args[1].split("@"); String host = args[0]; String vpnName = split[1]; String username = split[0]; String password = args[2]; System.out.printf("BasicRequestor is connecting to Solace messaging at %s...%n", host); // Programmatically create the connection factory using default settings SolConnectionFactory connectionFactory = SolJmsUtility.createConnectionFactory(); connectionFactory.setHost(host); connectionFactory.setVPN(vpnName); connectionFactory.setUsername(username); connectionFactory.setPassword(password); // Create connection to the Solace router Connection connection = connectionFactory.createConnection(); // Create a non-transacted, auto ACK session. Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); System.out.printf("Connected to the Solace Message VPN '%s' with client username '%s'.%n", vpnName, username); // Create the request topic programmatically Topic requestTopic = session.createTopic(REQUEST_TOPIC_NAME); // Create the message producer for the created queue MessageProducer requestProducer = session.createProducer(requestTopic); // The response will be received on this temporary queue. TemporaryQueue replyToQueue = session.createTemporaryQueue(); // Create consumer for receiving the request's reply MessageConsumer replyConsumer = session.createConsumer(replyToQueue); // Start receiving replies connection.start(); // Create a request. TextMessage request = session.createTextMessage("Sample Request"); // The application must put the destination of the reply in the replyTo field of the request request.setJMSReplyTo(replyToQueue); // The application must put a correlation ID in the request String correlationId = UUID.randomUUID().toString(); request.setJMSCorrelationID(correlationId); System.out.printf("Sending request '%s' to topic '%s'...%n", request.getText(), requestTopic.toString()); // Send the request requestProducer.send(requestTopic, request, DeliveryMode.NON_PERSISTENT, Message.DEFAULT_PRIORITY, Message.DEFAULT_TIME_TO_LIVE); System.out.println("Sent successfully. Waiting for reply..."); // the main thread blocks at the next statement until a message received or the timeout occurs Message reply = replyConsumer.receive(REPLY_TIMEOUT_MS); if (reply == null) { throw new Exception("Failed to receive a reply in " + REPLY_TIMEOUT_MS + " msecs"); } // Process the reply if (reply.getJMSCorrelationID() == null) { throw new Exception( "Received a reply message with no correlationID. This field is needed for a direct request."); } // Apache Qpid JMS prefixes correlation ID with string "ID:" so remove such prefix for interoperability if (!reply.getJMSCorrelationID().replaceAll("ID:", "").equals(correlationId)) { throw new Exception("Received invalid correlationID in reply message."); } if (reply instanceof TextMessage) { System.out.printf("TextMessage response received: '%s'%n", ((TextMessage) reply).getText()); if (!reply.getBooleanProperty(SupportedProperty.SOLACE_JMS_PROP_IS_REPLY_MESSAGE)) { System.out.println("Warning: Received a reply message without the isReplyMsg flag set."); } } else { System.out.println("Message response received."); } System.out.printf("Message Content:%n%s%n", SolJmsUtility.dumpMessage(reply)); connection.stop(); // Close everything in the order reversed from the opening order // NOTE: as the interfaces below extend AutoCloseable, // with them it's possible to use the "try-with-resources" Java statement // see details at https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html replyConsumer.close(); requestProducer.close(); session.close(); connection.close(); }
Example 16
Source File: JMSMessageConsumerTest.java From activemq-artemis with Apache License 2.0 | 4 votes |
@Test(timeout = 30000) public void testSelectorsWithJMSCorrelationID() throws Exception { Connection connection = createConnection(); final String correlationID = UUID.randomUUID().toString(); try { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); javax.jms.Queue queue = session.createQueue(getQueueName()); MessageProducer producer = session.createProducer(queue); TextMessage message1 = session.createTextMessage(); message1.setText("text"); producer.send(message1); TextMessage message2 = session.createTextMessage(); message2.setJMSCorrelationID(correlationID); message2.setText("JMSCorrelationID"); producer.send(message2); QueueBrowser browser = session.createBrowser(queue); Enumeration<?> enumeration = browser.getEnumeration(); int count = 0; while (enumeration.hasMoreElements()) { Message m = (Message) enumeration.nextElement(); assertTrue(m instanceof TextMessage); count++; } assertEquals(2, count); MessageConsumer consumer = session.createConsumer(queue, "JMSCorrelationID = '" + correlationID + "'"); Message msg = consumer.receive(2000); assertNotNull(msg); assertTrue(msg instanceof TextMessage); assertEquals("Unexpected JMSCorrelationID value", correlationID, msg.getJMSCorrelationID()); assertEquals("Unexpected message content", "JMSCorrelationID", ((TextMessage) msg).getText()); } finally { connection.close(); } }
Example 17
Source File: SelectorTest.java From activemq-artemis with Apache License 2.0 | 3 votes |
@Test public void testJMSCorrelationIDOnSelector() throws Exception { Connection conn = null; try { conn = getConnectionFactory().createConnection(); conn.start(); Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer prod = session.createProducer(queue1); TextMessage msg1 = session.createTextMessage("msg1"); msg1.setJMSCorrelationID("cid1"); prod.send(msg1); TextMessage msg2 = session.createTextMessage("msg2"); msg2.setJMSCorrelationID("cid2"); prod.send(msg2); String selector = "JMSCorrelationID = 'cid2'"; MessageConsumer cons = session.createConsumer(queue1, selector); conn.start(); TextMessage rec = (TextMessage) cons.receive(10000); assertNotNull(rec); Assert.assertEquals("msg2", rec.getText()); assertNull(cons.receiveNoWait()); } finally { if (conn != null) { conn.close(); } } }