public void start() throws JMSException { if (started.compareAndSet(false, true)) { session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); queueConsumer = session.createConsumer(AdvisorySupport.QUEUE_ADVISORY_TOPIC); queueConsumer.setMessageListener(this); topicConsumer = session.createConsumer(AdvisorySupport.TOPIC_ADVISORY_TOPIC); topicConsumer.setMessageListener(this); tempQueueConsumer = session.createConsumer(AdvisorySupport.TEMP_QUEUE_ADVISORY_TOPIC); tempQueueConsumer.setMessageListener(this); tempTopicConsumer = session.createConsumer(AdvisorySupport.TEMP_TOPIC_ADVISORY_TOPIC); tempTopicConsumer.setMessageListener(this); } }
Destination dest = jmsProvider.destination(); this.connection = cf.createConnection(); this.session = connection.createSession(false, jmsAcknowledgeMode); MessageConsumer consumer = session.createConsumer(dest); consumer.setMessageListener(this); this.connection.start();
try { responseQueue = session.createTemporaryQueue(); producer = session.createProducer(queue); consumer = session.createConsumer(responseQueue); requestMessage.setJMSReplyTo(responseQueue); producer.send(requestMessage); long timeout = getReceiveTimeout(); return (timeout > 0 ? consumer.receive(timeout) : consumer.receive());
@Test public void testProducerAndConsume_TagFilter() throws Exception { Connection connection = createConnection(producerId, consumerId); Connection anotherConnection = createConnection(producerId, consumerId +"other"); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Session anotherSession = anotherConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); JmsTestListener listenerForAll = new JmsTestListener(40, countDownLatch); try { session.createConsumer(destinationA).setMessageListener(listenerForTagA); anotherSession.createConsumer(session.createTopic("topic")).setMessageListener(listenerForAll); MessageProducer messageProducer = session.createProducer(destinationA); connection.start(); anotherConnection.start(); TextMessage message = session.createTextMessage(text + i); Assert.assertNull(message.getJMSMessageID()); messageProducer.send(message); Assert.assertNotNull(message.getJMSMessageID()); TextMessage message = session.createTextMessage(text + i); Assert.assertNull(message.getJMSMessageID()); messageProducer.send(destinationB, message); Assert.assertNotNull(message.getJMSMessageID());
@Test public void testStartIdempotency() throws Exception { JmsBaseConnectionFactory connectionFactory = new JmsBaseConnectionFactory(new URI("rocketmq://xxx?consumerId=" + consumerId + "&nameServer=" + nameServer)); Connection connection = connectionFactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); checkConsumerState(consumerId, true, false); try { Destination destination = session.createTopic(topic + ":" + messageType); MessageConsumer consumer = session.createConsumer(destination); consumer.setMessageListener(listener); checkConsumerState(consumerId, false, false); ((JmsBaseMessageConsumer) consumer).startConsumer(); checkConsumerState(consumerId, false, true); Destination destination1 = session.createTopic(topic2 + ":" + messageType); MessageConsumer consumer1 = session.createConsumer(destination1); consumer1.setMessageListener(listener); ((JmsBaseMessageConsumer) consumer1).startConsumer(); checkConsumerState(consumerId, false, true); //the start is idempotent connection.start(); connection.start(); Thread.sleep(5000); } finally { connection.close(); } }
@Test public void testDontStart() throws Exception { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); ActiveMQDestination destination = createDestination(session, destinationType); MessageConsumer consumer = session.createConsumer(destination); // Send the messages sendMessages(session, destination, 1); // Make sure no messages were delivered. assertNull(consumer.receive(1000)); }
@Test public void testContextRefreshedEventDoesNotStartTheConnectionIfAutoStartIsSetToFalse() throws Exception { MessageConsumer messageConsumer = mock(MessageConsumer.class); Session session = mock(Session.class); // Queue gets created in order to create MessageConsumer for that Destination... given(session.createQueue(DESTINATION_NAME)).willReturn(QUEUE_DESTINATION); // and then the MessageConsumer gets created... given(session.createConsumer(QUEUE_DESTINATION, null)).willReturn(messageConsumer); // no MessageSelector... Connection connection = mock(Connection.class); // session gets created in order to register MessageListener... given(connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode())).willReturn(session); ConnectionFactory connectionFactory = mock(ConnectionFactory.class); given(connectionFactory.createConnection()).willReturn(connection); this.container.setConnectionFactory(connectionFactory); this.container.setDestinationName(DESTINATION_NAME); this.container.setMessageListener(new TestMessageListener()); this.container.setAutoStartup(false); this.container.afterPropertiesSet(); GenericApplicationContext context = new GenericApplicationContext(); context.getBeanFactory().registerSingleton("messageListenerContainer", this.container); context.refresh(); verify(connection).setExceptionListener(this.container); }
@SuppressWarnings("serial") @Test public void testReplyContainerRecovery() throws Exception { JmsOutboundGateway gateway = new JmsOutboundGateway(); }).when(connectionFactory).createConnection(); Session session = mock(Session.class); when(connection.createSession(false, 1)).thenReturn(session); MessageConsumer consumer = mock(MessageConsumer.class); when(session.createConsumer(any(Destination.class), isNull())).thenReturn(consumer); when(session.createTemporaryQueue()).thenReturn(mock(TemporaryQueue.class)); final Message message = mock(Message.class); }).when(consumer).receive(anyLong()); when(message.getJMSCorrelationID()).thenReturn("foo"); DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
private int clearQueue(Connection connection, String queueName) throws Exception { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); org.apache.activemq.command.ActiveMQQueue destination = (org.apache.activemq.command.ActiveMQQueue) session.createQueue(queueName); MessageConsumer messageConsumer = session.createConsumer(destination); int received = 0; while (messageConsumer.receive(500) != null) received++; session.close(); return received; }
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(); }
TemporaryQueue replyDestination = mock(TemporaryQueue.class); MessageProducer messageProducer = mock(MessageProducer.class); given(localSession.createProducer(this.queue)).willReturn(messageProducer); given(localSession.createTemporaryQueue()).willReturn(replyDestination); given(localSession.createConsumer(replyDestination)).willReturn(messageConsumer); given(messageConsumer.receive()).willReturn(reply); given(messageConsumer.receive(timeout)).willReturn(reply); verify(this.connection).start(); verify(this.connection).close(); verify(localSession).close();
private void checkDestination(String name) throws Exception { ConnectionFactory cf = (ConnectionFactory) namingContext.lookup("/someCF"); Connection conn = cf.createConnection(); Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination dest = (Destination) namingContext.lookup(name); conn.start(); MessageConsumer cons = sess.createConsumer(dest); MessageProducer prod = sess.createProducer(dest); prod.send(sess.createMessage()); assertNotNull(cons.receiveNoWait()); conn.close(); }
public List<Message> receive(Queue queue) throws Exception { List<Message> messages = new ArrayList<Message>(); Connection qconnetion = factory.createConnection(); Session qsession = qconnetion.createSession(true, QueueSession.AUTO_ACKNOWLEDGE); MessageConsumer consumer = qsession.createConsumer(queue); qconnetion.start(); Message m = null; while ((m = consumer.receiveNoWait()) != null) { messages.add(m); } consumer.close(); qsession.close(); qconnetion.close(); return messages; } }
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());
protected synchronized void initSession() throws JMSException { if (producerSession == null || consumerSession == null) { producerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); consumerSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); queue = producerSession.createQueue(getName()); producer = producerSession.createProducer(queue); consumer = consumerSession.createConsumer(queue); } }
Message requestMessage = messageCreator.createMessage(session); responseQueue = session.createTemporaryQueue(); producer = session.createProducer(destination); consumer = session.createConsumer(responseQueue); requestMessage.setJMSReplyTo(responseQueue); if (logger.isDebugEnabled()) {
consumer = session.createDurableSubscriber((Topic) destination, getName()); } else { consumer = session.createConsumer(destination); Message msg = consumer.receive(receiveTimeOut); if (msg != null) { LOG.info(threadName + " Received " + (msg instanceof TextMessage ? ((TextMessage) msg).getText() : msg.getJMSMessageID()));
@Test public void testProducerAndConsume_TwoConsumer() throws Exception { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Destination destinationA = session.createTopic("TopicA"); Destination destinationB = session.createTopic("TopicB"); MessageConsumer messageConsumerA = session.createConsumer(destinationA); messageConsumerA.setMessageListener(listenerA); MessageConsumer messageConsumerB = session.createConsumer(destinationB); messageConsumerB.setMessageListener(listenerB); MessageProducer messageProducer = session.createProducer(destinationA); connection.start(); TextMessage message = session.createTextMessage(text + i); Assert.assertNull(message.getJMSMessageID()); messageProducer.send(message); Assert.assertNotNull(message.getJMSMessageID()); TextMessage message = session.createTextMessage(text + i); Assert.assertNull(message.getJMSMessageID()); messageProducer.send(destinationB, message); Assert.assertNotNull(message.getJMSMessageID());
@Override protected void setUp() throws Exception { brokerService = new BrokerService(); brokerService.setPersistent(false); brokerService.start(); factory = new ActiveMQConnectionFactory(BrokerRegistry.getInstance().findFirst().getVmConnectorURI()); consumerConnection = factory.createConnection(); consumerConnection.start(); producerConnection = factory.createConnection(); producerConnection.start(); consumerSession = consumerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE); topic = consumerSession.createTopic(getName()); producerSession = producerConnection.createSession(false,Session.AUTO_ACKNOWLEDGE); consumer = consumerSession.createConsumer(topic); producer = producerSession.createProducer(topic); }
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(); }