/** Sends a request and waits for a reply. The temporary topic is used for * the {@code JMSReplyTo} destination; the first reply is returned, * and any following replies are discarded. * * @param message the message to send * * @return the reply message * * @exception JMSException if the JMS provider fails to complete the * request due to some internal error. */ public Message request(Message message) throws JMSException { message.setJMSReplyTo(tempTopic); publisher.publish(message); return(subscriber.receive()); }
public JMSSink( String tcfBindingName, String topicBindingName, String username, String password) { try { Context ctx = new InitialContext(); TopicConnectionFactory topicConnectionFactory; topicConnectionFactory = (TopicConnectionFactory) lookup(ctx, tcfBindingName); TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(username, password); topicConnection.start(); TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = (Topic)ctx.lookup(topicBindingName); TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic); topicSubscriber.setMessageListener(this); } catch(JMSException e) { logger.error("Could not read JMS message.", e); } catch(NamingException e) { logger.error("Could not read JMS message.", e); } catch(RuntimeException e) { logger.error("Could not read JMS message.", e); } }
@Override @Nullable public Topic getTopic() throws JMSException { return (this.target instanceof TopicSubscriber ? ((TopicSubscriber) this.target).getTopic() : null); }
@Test public void testDurableSubscriptionWithPeriodsInName() throws Exception { Connection conn = createConnection(); conn.setClientID(".client.id.with.periods."); Session s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); TopicSubscriber subscriber = s.createDurableSubscriber(ActiveMQServerTestCase.topic1, ".subscription.name.with.periods."); s.createProducer(ActiveMQServerTestCase.topic1).send(s.createTextMessage("Subscription test")); conn.start(); Message m = subscriber.receive(1000L); ProxyAssertSupport.assertNotNull(m); ProxyAssertSupport.assertTrue(m instanceof TextMessage); subscriber.close(); s.unsubscribe(".subscription.name.with.periods."); }
@Test public void testDurableSub() throws Exception { Connection conn = factory.createConnection(); try { conn.setClientID("my-client"); conn.start(); Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic("p.IN,q.IN"); TopicSubscriber sub1 = session.createDurableSubscriber(topic, "durable1", null, false); MessageProducer producer = session.createProducer(topic); producer.send(session.createTextMessage("msg" + i)); TextMessage msg = (TextMessage) sub1.receive(2000); while (msg != null) { count++; msg = (TextMessage) sub1.receive(2000);
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic(getTopicName()); TopicSubscriber sub = session.createDurableSubscriber(topic, "myPubId"); Session sendSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = sendSession.createProducer(topic); connection.start(); producer.send(sendSession.createTextMessage("message:" + i)); TextMessage receive = (TextMessage) sub.receive(5000); Assert.assertNotNull(receive); Assert.assertEquals(receive.getText(), "message:" + i); connection.start(); for (int i = 0; i < numMessages; i++) { producer.send(sendSession.createTextMessage("message:" + i)); TextMessage receive = (TextMessage) sub.receive(5000); Assert.assertNotNull(receive); Assert.assertEquals(receive.getText(), "message:" + i);
TopicConnection conn1 = ((TopicConnectionFactory) topicCf).createTopicConnection(); TopicConnection conn2 = ((TopicConnectionFactory) topicCf).createTopicConnection(); TopicSession sess1 = conn1.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TopicSession sess2 = conn2.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TopicSubscriber sub1 = sess1.createSubscriber(ActiveMQServerTestCase.topic1); TopicSubscriber sub2 = sess2.createSubscriber(ActiveMQServerTestCase.topic1); conn1.start(); TextMessage tm = sess3.createTextMessage("hello"); prod.send(tm); TextMessage tm = (TextMessage) sub1.receive(500); ProxyAssertSupport.assertNotNull(tm); ProxyAssertSupport.assertEquals("hello", tm.getText()); Message m = sub2.receive(200); TextMessage tm = (TextMessage) sub2.receive(500); ProxyAssertSupport.assertNotNull(tm); ProxyAssertSupport.assertEquals("hello", tm.getText()); conn2.close(); conn3.close();
@Test(timeout = 60000) public void testSendAndReceiveOnTopic() throws Exception { Connection connection = createConnection("myClientId"); try { TopicSession session = (TopicSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic(getTopicName()); TopicSubscriber consumer = session.createSubscriber(topic); TopicPublisher producer = session.createPublisher(topic); TextMessage message = session.createTextMessage("test-message"); producer.send(message); producer.close(); connection.start(); message = (TextMessage) consumer.receive(1000); assertNotNull(message); assertNotNull(message.getText()); assertEquals("test-message", message.getText()); } finally { connection.close(); } }
TopicSession session = (TopicSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic(getTopicName()); TopicSubscriber sub1 = session.createDurableSubscriber(topic, "myPubId1"); TopicSubscriber sub2 = session.createDurableSubscriber(topic, "myPubId2"); TopicSubscriber sub3 = session.createDurableSubscriber(topic, "myPubId3"); Session sendSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = sendSession.createProducer(topic); connection.start(); for (int i = 0; i < numMessages; i++) { producer.send(sendSession.createTextMessage("message:" + i)); TextMessage receive = (TextMessage) sub1.receive(5000); Assert.assertNotNull(receive); Assert.assertEquals(receive.getText(), "message:" + i); receive = (TextMessage) sub2.receive(5000); Assert.assertNotNull(receive); Assert.assertEquals(receive.getText(), "message:" + i); receive = (TextMessage) sub3.receive(5000); Assert.assertNotNull(receive); Assert.assertEquals(receive.getText(), "message:" + i);
@Test(timeout = 60000) public void testDurableSubscriptionUnsubscribe() throws Exception { Connection connection = createConnection("myClientId"); try { Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic(getTopicName()); TopicSubscriber myDurSub = session.createDurableSubscriber(topic, "myDurSub"); session.close(); connection.close(); connection = createConnection("myClientId"); session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); myDurSub = session.createDurableSubscriber(topic, "myDurSub"); myDurSub.close(); Assert.assertNotNull(server.getPostOffice().getBinding(new SimpleString("myClientId.myDurSub"))); session.unsubscribe("myDurSub"); Assert.assertNull(server.getPostOffice().getBinding(new SimpleString("myClientId.myDurSub"))); session.close(); connection.close(); } finally { connection.close(); } }
@Test public void testGetTopic() throws Exception { Connection consumerConnection = null; try { consumerConnection = createConnection(); Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer topicConsumer = consumerSession.createConsumer(ActiveMQServerTestCase.topic1); Topic t = ((TopicSubscriber) topicConsumer).getTopic(); ProxyAssertSupport.assertEquals(ActiveMQServerTestCase.topic1, t); } finally { if (consumerConnection != null) { consumerConnection.close(); } } }
protected void consumeDurableTestMessages(Connection connection, String sub, int size, String topicName, AtomicLong publishedMessageSize) throws Exception { Session session = connection.createSession(false, QueueSession.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic(topicName); try { TopicSubscriber consumer = session.createDurableSubscriber(topic, sub); for (int i = 0; i < size; i++) { ActiveMQMessage message = (ActiveMQMessage) consumer.receive(); if (publishedMessageSize != null) { publishedMessageSize.addAndGet(-message.getCoreMessage().getEncodeSize()); } } } finally { session.close(); } }
@Test public void testUnsubscribeWithActiveConsumer() throws Exception { Connection conn = createConnection(); conn.setClientID("zeke"); Session s = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); TopicSubscriber dursub = s.createDurableSubscriber(ActiveMQServerTestCase.topic1, "dursub0"); try { s.unsubscribe("dursub0"); ProxyAssertSupport.fail(); } catch (IllegalStateException e) { // Ok - it is illegal to ubscribe a subscription if it has active consumers } dursub.close(); s.unsubscribe("dursub0"); }
protected void connect() { try { Context ctx = newContext(); TopicConnectionFactory tcf = (TopicConnectionFactory) ctx.lookup(_tcfName); Topic topic = (Topic) ctx.lookup(_topicName); ctx.close(); _connection = tcf.createTopicConnection(); // false == not transacted. _session = _connection.createTopicSession (false, Session.AUTO_ACKNOWLEDGE); // create a publisher _publisher = _session.createPublisher(topic); // create a subscriber. TopicSubscriber s = _session.createSubscriber(topic, null, /* noLocal: */ true); MessageListener l = getMessageListener(); s.setMessageListener(l); _connection.start(); _connection.setExceptionListener(this); if (log.isInfoEnabled()) log.info(s_loc.get("jms-start-listener", _topicName)); } catch (OpenJPAException ke) { throw ke; } catch (Exception e) { throw new UserException(s_loc.get("jms-provider-config", _topicName, _tcfName), e).setFatal(true); } }
@Test public void testGetNoLocalOnClosedConsumer() throws Exception { Connection consumerConnection = null; try { consumerConnection = createConnection(); TopicConnection tc = (TopicConnection) consumerConnection; TopicSession consumerSession = tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); TopicSubscriber topicConsumer = consumerSession.createSubscriber(ActiveMQServerTestCase.topic1); topicConsumer.close(); try { topicConsumer.getNoLocal(); Assert.fail("must throw a JMS IllegalStateException"); } catch (javax.jms.IllegalStateException e) { // OK } } finally { if (consumerConnection != null) { consumerConnection.close(); } } }
/** * Topics shouldn't hold on to messages when the non-durable subscribers close */ @Test public void testPersistentMessagesForTopicDropped2() throws Exception { TopicConnection topicConn = createTopicConnection(); topicConn.start(); TopicSession sess = topicConn.createTopicSession(true, 0); TopicPublisher pub = sess.createPublisher(ActiveMQServerTestCase.topic1); TopicSubscriber sub = sess.createSubscriber(ActiveMQServerTestCase.topic1); pub.setDeliveryMode(DeliveryMode.PERSISTENT); Message m = sess.createTextMessage("testing123"); pub.publish(m); sess.commit(); // receive but rollback TextMessage m2 = (TextMessage) sub.receive(3000); ProxyAssertSupport.assertNotNull(m2); ProxyAssertSupport.assertEquals("testing123", m2.getText()); sess.rollback(); topicConn.close(); checkEmpty(ActiveMQServerTestCase.topic1); }
/** * Test that a durable subscriber effectively receives the messages sent to its * topic while it was inactive. */ @Test public void testDurableSubscriber() { try { subscriber = subscriberSession.createDurableSubscriber(subscriberTopic, "testTopic"); subscriberConnection.close(); subscriberConnection = null; TextMessage message = publisherSession.createTextMessage(); message.setText("test"); publisher.publish(message); subscriberConnection = subscriberTCF.createTopicConnection(); subscriberConnection.setClientID("subscriberConnection"); subscriberSession = subscriberConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); subscriber = subscriberSession.createDurableSubscriber(subscriberTopic, "testTopic"); subscriberConnection.start(); TextMessage m = (TextMessage) subscriber.receive(TestConfig.TIMEOUT); Assert.assertTrue(m != null); Assert.assertEquals("test", m.getText()); subscriber.close(); subscriberSession.unsubscribe("testTopic"); } catch (JMSException e) { fail(e); } }
@Test public void testNotificationProperties() throws Exception { try (TopicConnection topicConnection = factory.createTopicConnection()) { TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic notificationsTopic = topicSession.createTopic("activemq.notifications"); TopicSubscriber subscriber = topicSession.createSubscriber(notificationsTopic); List<Message> receivedMessages = new CopyOnWriteArrayList<>(); subscriber.setMessageListener(receivedMessages::add); topicConnection.start(); Wait.waitFor(() -> receivedMessages.size() > 0); Assert.assertTrue(receivedMessages.size() > 0); for (Message message : receivedMessages) { assertNotNull(message); assertNotNull(message.getStringProperty("_AMQ_NotifType")); } } }
public Message request(Message message) throws JMSException { TemporaryTopic tempTopic = _session.createTemporaryTopic(); try { message.setJMSReplyTo(tempTopic); _sender.publish(message); TopicSubscriber receiver = _session.createSubscriber(tempTopic); try { return receiver.receive(); } finally { receiver.close(); } } finally { tempTopic.delete(); } }