public void testCloseConsumer() throws Exception { Destination dest = session.createQueue(getSubject() + "?consumer.prefetchSize=0"); producer = session.createProducer(dest); beginTx(); producer.send(session.createTextMessage("message 1")); producer.send(session.createTextMessage("message 2")); commitTx(); beginTx(); consumer = session.createConsumer(dest); Message message1 = consumer.receive(1000); String text1 = ((TextMessage)message1).getText(); assertNotNull(message1); assertEquals("message 1", text1); consumer.close(); consumer = session.createConsumer(dest); Message message2 = consumer.receive(1000); String text2 = ((TextMessage)message2).getText(); assertNotNull(message2); assertEquals("message 2", text2); commitTx(); }
MessageProducer producer = null; try { queueConnection = connectionFactory.createConnection(); queueSession = queueConnection.createSession(transacted, Session.AUTO_ACKNOWLEDGE); TextMessage message = queueSession.createTextMessage(eventXml); message.setIntProperty("EventType", eventType); message.setStringProperty("LogType", "Process"); producer = queueSession.createProducer(queue); producer.setPriority(priority); producer.send(message); } catch (Exception e) { throw new RuntimeException("Error when sending JMS message with working memory event", e); if (producer != null) { try { producer.close(); } catch (JMSException e) { logger.warn("Error when closing producer", e); queueSession.close(); } catch (JMSException e) { logger.warn("Error when closing queue session", e); queueConnection.close(); } catch (JMSException e) { logger.warn("Error when closing queue connection", e);
@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); }
private void assertTextMessage(MessageCreator messageCreator) { try { TextMessage jmsMessage = createTextMessage(messageCreator); assertEquals("Wrong body message", "Hello", jmsMessage.getText()); assertEquals("Invalid foo property", "bar", jmsMessage.getStringProperty("foo")); } catch (JMSException e) { throw new IllegalStateException("Wrong text message", e); } }
@Test public void testTopic2() throws Exception { Connection conn = createConnection(); Session s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer p = s.createProducer(ActiveMQServerTestCase.topic1); MessageConsumer c = s.createConsumer(ActiveMQServerTestCase.topic1); conn.start(); p.send(s.createTextMessage("payload")); TextMessage m = (TextMessage) c.receive(); ProxyAssertSupport.assertEquals("payload", m.getText()); }
private static void receiveMessage() throws Exception { // 创建 ActiveMQ 链接,设置 Broker URL ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616"); // 创造 JMS 链接 Connection connection = connectionFactory.createConnection(); // 启动连接 connection.start(); // 创建会话 Session Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); // 创建消息目的 - Queue 名称为 "TEST" Destination destination = session.createQueue("TEST"); // 创建消息消费者 MessageConsumer messageConsumer = session.createConsumer(destination); // 获取消息 Message message = messageConsumer.receive(100); if (message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; System.out.println("消息消费内容:" + textMessage.getText()); } // 关闭消息消费者 messageConsumer.close(); // 关闭会话 session.close(); // 关闭连接 connection.stop(); connection.close(); }
@Test public void testAutoSend() throws Exception { connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue queue = session.createQueue(queueName); MessageConsumer consumer = session.createConsumer(queue); MessageProducer producer = session.createProducer(queue); for (int i = 0; i < 10; i++) { producer.send(session.createTextMessage("testXX" + i)); } connection.start(); for (int i = 0; i < 10; i++) { TextMessage txt = (TextMessage) consumer.receive(5000); Assert.assertEquals("testXX" + i, txt.getText()); } }
private void receiveLVQ(ConnectionSupplier consumerConnectionSupplier, String queueName, String lastValueKey) throws JMSException { try (Connection consumerConnection = consumerConnectionSupplier.createConnection()) { Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue consumerQueue = consumerSession.createQueue(queueName); MessageConsumer consumer = consumerSession.createConsumer(consumerQueue); TextMessage msg = (TextMessage) consumer.receive(1000); assertNotNull(msg); assertEquals("KEY", msg.getStringProperty(lastValueKey)); assertEquals("how are you", msg.getText()); consumer.close(); } }
private void sendLVQ(ConnectionSupplier producerConnectionSupplier, String queueName, String lastValueKey) throws JMSException { try (Connection connection = producerConnectionSupplier.createConnection(); Session session = connection.createSession(); MessageProducer producer = session.createProducer(session.createQueue(queueName))) { TextMessage message1 = session.createTextMessage(); message1.setStringProperty(lastValueKey, "KEY"); message1.setText("hello"); producer.send(message1); TextMessage message2 = session.createTextMessage(); message2.setStringProperty(lastValueKey, "KEY"); message2.setText("how are you"); producer.send(message2); } }
public void sendMessage(SimpleString queue) throws Exception { ConnectionFactory fact = getCF(); Connection connection = fact.createConnection(); try { Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE); connection.start(); Destination destination = session.createQueue(queue.toString()); MessageProducer producer = session.createProducer(destination); TextMessage message = session.createTextMessage(); message.setText("Message"); producer.send(message); } finally { connection.close(); } }
given(this.session.createConsumer(this.queue, messageSelector ? selectorString : null)).willReturn(messageConsumer); given(this.session.getAcknowledgeMode()).willReturn( clientAcknowledge ? Session.CLIENT_ACKNOWLEDGE : Session.AUTO_ACKNOWLEDGE); given(textMessage.getText()).willReturn("Hello World!"); given(messageConsumer.receiveNoWait()).willReturn(textMessage); given(messageConsumer.receive()).willReturn(textMessage); given(messageConsumer.receive(timeout)).willReturn(textMessage); verify(this.connection).start(); verify(this.connection).close(); if (useTransactedTemplate()) { verify(this.session).commit(); verify(this.session).close(); if (!useTransactedSession() && clientAcknowledge) { verify(textMessage).acknowledge(); verify(messageConsumer).close();
@Test(timeout = 60000) public void testCreateTemporaryTopic() throws Throwable { Connection connection = createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); TemporaryTopic topic = session.createTemporaryTopic(); System.out.println("topic:" + topic.getTopicName()); MessageConsumer consumer = session.createConsumer(topic); MessageProducer producer = session.createProducer(topic); TextMessage message = session.createTextMessage(); message.setText("Message temporary"); producer.send(message); connection.start(); message = (TextMessage) consumer.receive(5000); assertNotNull(message); }
@Test public void testRollbackLocal() throws Exception { connection.start(); Session session = connection.createSession(true, Session.SESSION_TRANSACTED); Queue queue = session.createQueue(queueName); MessageConsumer consumer = session.createConsumer(queue); MessageProducer producer = session.createProducer(queue); for (int i = 0; i < 10; i++) { TextMessage msg = session.createTextMessage("testXX" + i); msg.setStringProperty("count", "str " + i); producer.send(msg); } session.commit(); connection.start(); for (int i = 0; i < 5; i++) { TextMessage txt = (TextMessage) consumer.receive(500); Assert.assertEquals("testXX" + i, txt.getText()); } session.rollback(); for (int i = 0; i < 10; i++) { TextMessage txt = (TextMessage) consumer.receive(5000); Assert.assertNotNull(txt); System.out.println("TXT " + txt.getText()); Assert.assertEquals("testXX" + i, txt.getText()); } checkDuplicate(consumer); session.commit(); }
/** * Sends a batch of messages and validates that the messages are received. * * @throws Exception */ public void testSendReceiveTransactedBatches() throws Exception { TextMessage message = session.createTextMessage("Batch Message"); for (int j = 0; j < batchCount; j++) { LOG.info("Producing bacth " + j + " of " + batchSize + " messages"); beginTx(); for (int i = 0; i < batchSize; i++) { producer.send(message); } messageSent(); commitTx(); LOG.info("Consuming bacth " + j + " of " + batchSize + " messages"); beginTx(); for (int i = 0; i < batchSize; i++) { message = (TextMessage)consumer.receive(1000 * 5); assertNotNull("Received only " + i + " messages in batch " + j, message); assertEquals("Batch Message", message.getText()); } commitTx(); } }
@Override public Object execute() throws Exception { Connection connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destination = session.createQueue(queue); MessageConsumer consumer = session.createConsumer(destination); TextMessage message = (TextMessage) consumer.receive(60000); if (message == null) { throw new IllegalStateException("No message received"); } else { System.out.println(message.getText()); } return null; }
private void sendTextMessages(int nMsgs, ConnectionFactory factory) throws Exception { try (Connection connection = factory.createConnection()) { Session session = connection.createSession(); Queue queue = session.createQueue(testQueueName); MessageProducer producer = session.createProducer(queue); TextMessage msg = session.createTextMessage(); StringBuilder builder = new StringBuilder(); for (int i = 0; i < PAYLOAD; ++i) { builder.append("A"); } msg.setText(builder.toString()); for (int i = 0; i < nMsgs; ++i) { msg.setIntProperty("i", (Integer) i); producer.send(msg); } } }
final ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); final javax.jms.Connection connection = connectionFactory.createConnection(); connection.setClientID("12345"); connection.start(); final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); final Topic temporaryTopic = session.createTemporaryTopic(); final MessageConsumer consumer1 = session.createConsumer(temporaryTopic); final MessageProducer producer = session.createProducer(temporaryTopic); producer.send(session.createTextMessage("Testmessage")); final TextMessage message = (TextMessage)consumer1.receiveNoWait(); Assert.assertNotNull(message); Assert.assertEquals("testing", message.getText());
@Test public void testDurableConsumerSelectorChange() throws Exception { TextMessage message = session.createTextMessage("1st"); message.setStringProperty("color", "red"); producer.send(message); Message m = consumer.receive(1000); assertNotNull(m); assertEquals("1st", ((TextMessage) m).getText()); consumer.close(); consumer = session.createDurableSubscriber((Topic) destination, "test", "color='blue'", false); message = session.createTextMessage("2nd"); message.setStringProperty("color", "red"); producer.send(message); message = session.createTextMessage("3rd"); message.setStringProperty("color", "blue"); producer.send(message); m = consumer.receive(1000); assertNotNull(m); assertEquals("3rd", ((TextMessage) m).getText()); assertNull(consumer.receiveNoWait());
public void onMessage(javax.jms.Message request, Session session) throws JMSException { String text = "priority=" + request.getJMSPriority(); TextMessage reply = session.createTextMessage(text); MessageProducer producer = session.createProducer(request.getJMSReplyTo()); reply.setJMSCorrelationID(request.getJMSMessageID()); producer.send(reply); } }
private void sendToDestination(String message, Destination destination) { Connection connection = null; try { connection = connectionFactory.createConnection(); final Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); final javax.jms.MessageProducer producer = TraceeMessageWriter.wrap(session.createProducer(destination)); final TextMessage textMessage = session.createTextMessage(); textMessage.setText(message); LOG.info("I am about to send the message \"{]\" to {}", message, destination.toString()); producer.send(textMessage); session.commit(); } catch (JMSException jmse) { throw new RuntimeException("This example is so cheap, this must not have happened!", jmse); } }