List of usage examples for javax.jms TopicSubscriber receive
Message receive() throws JMSException;
From source file:org.apache.flink.streaming.connectors.jms.JmsTopicSource.java
@Override public void run(final SourceContext<T> context) throws Exception { TopicSession session = null;//from w w w . j ava2 s. c o m TopicSubscriber consumer = null; try { session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); consumer = session.createSubscriber(destination, messageSelector, false); connection.start(); while (isRunning) { context.collect(convert(consumer.receive())); } } catch (JMSException e) { logger.error("Error receiving message from [{}]: {}", destination.getTopicName(), e.getLocalizedMessage()); throw new UncategorizedJmsException(e); } finally { JmsUtils.closeMessageConsumer(consumer); JmsUtils.closeSession(session); } }
From source file:org.apache.activemq.bugs.AMQ6131Test.java
@Test(timeout = 300000) public void testDurableWithOnePendingAfterRestartAndIndexRecovery() throws Exception { final File persistentDir = getPersistentDir(); broker.getBroker().addDestination(broker.getAdminConnectionContext(), new ActiveMQTopic("durable.sub"), false);/*from www . j av a 2 s .c o m*/ ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(this.brokerConnectURI); ActiveMQConnection connection = (ActiveMQConnection) connectionFactory.createConnection(); connection.setClientID("myId"); connection.start(); final Session jmsSession = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); TopicSubscriber durable = jmsSession.createDurableSubscriber(new ActiveMQTopic("durable.sub"), "sub"); final MessageProducer producer = jmsSession.createProducer(new ActiveMQTopic("durable.sub")); final int original = new ArrayList<File>( FileUtils.listFiles(persistentDir, new WildcardFileFilter("*.log"), TrueFileFilter.INSTANCE)) .size(); // 100k messages final byte[] data = new byte[100000]; final Random random = new Random(); random.nextBytes(data); // run test with enough messages to create a second journal file final AtomicInteger messageCount = new AtomicInteger(); assertTrue("Should have added a journal file", Wait.waitFor(new Condition() { @Override public boolean isSatisified() throws Exception { final ActiveMQBytesMessage message = new ActiveMQBytesMessage(); message.setContent(new ByteSequence(data)); for (int i = 0; i < 100; i++) { producer.send(message); messageCount.getAndIncrement(); } return new ArrayList<File>(FileUtils.listFiles(persistentDir, new WildcardFileFilter("*.log"), TrueFileFilter.INSTANCE)).size() > original; } })); // Consume all but 1 message for (int i = 0; i < messageCount.get() - 1; i++) { durable.receive(); } durable.close(); // wait until a journal file has been GC'd after receiving messages assertTrue("Subscription should go inactive", Wait.waitFor(new Condition() { @Override public boolean isSatisified() throws Exception { return broker.getAdminView().getInactiveDurableTopicSubscribers().length == 1; } })); // force a GC of unneeded journal files getBroker().getPersistenceAdapter().checkpoint(true); // wait until a journal file has been GC'd after receiving messages assertFalse("Should not have garbage collected", Wait.waitFor(new Wait.Condition() { @Override public boolean isSatisified() throws Exception { return new ArrayList<File>(FileUtils.listFiles(persistentDir, new WildcardFileFilter("*.log"), TrueFileFilter.INSTANCE)).size() == original; } }, 5000, 500)); // stop the broker so we can blow away the index getBroker().stop(); getBroker().waitUntilStopped(); // delete the index so that the durables are gone from the index // The test passes if you take out this delete section for (File index : FileUtils.listFiles(persistentDir, new WildcardFileFilter("db.*"), TrueFileFilter.INSTANCE)) { FileUtils.deleteQuietly(index); } stopBroker(); setUpBroker(false); assertEquals(1, broker.getAdminView().getInactiveDurableTopicSubscribers().length); assertEquals(0, broker.getAdminView().getDurableTopicSubscribers().length); ActiveMQConnectionFactory connectionFactory2 = new ActiveMQConnectionFactory(this.brokerConnectURI); ActiveMQConnection connection2 = (ActiveMQConnection) connectionFactory2.createConnection(); connection2.setClientID("myId"); connection2.start(); final Session jmsSession2 = connection2.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); TopicSubscriber durable2 = jmsSession2.createDurableSubscriber(new ActiveMQTopic("durable.sub"), "sub"); assertEquals(0, broker.getAdminView().getInactiveDurableTopicSubscribers().length); assertEquals(1, broker.getAdminView().getDurableTopicSubscribers().length); assertNotNull(durable2.receive(5000)); }
From source file:org.apache.activemq.bugs.AMQ6131Test.java
@Test(timeout = 300000) public void testDurableWithNoMessageAfterRestartAndIndexRecovery() throws Exception { final File persistentDir = getPersistentDir(); broker.getBroker().addDestination(broker.getAdminConnectionContext(), new ActiveMQTopic("durable.sub"), false);/*from ww w . j a v a2 s . co m*/ ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(this.brokerConnectURI); ActiveMQConnection connection = (ActiveMQConnection) connectionFactory.createConnection(); connection.setClientID("myId"); connection.start(); final Session jmsSession = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); TopicSubscriber durable = jmsSession.createDurableSubscriber(new ActiveMQTopic("durable.sub"), "sub"); final MessageProducer producer = jmsSession.createProducer(new ActiveMQTopic("durable.sub")); final int original = new ArrayList<File>( FileUtils.listFiles(persistentDir, new WildcardFileFilter("*.log"), TrueFileFilter.INSTANCE)) .size(); // 100k messages final byte[] data = new byte[100000]; final Random random = new Random(); random.nextBytes(data); // run test with enough messages to create a second journal file final AtomicInteger messageCount = new AtomicInteger(); assertTrue("Should have added a journal file", Wait.waitFor(new Condition() { @Override public boolean isSatisified() throws Exception { final ActiveMQBytesMessage message = new ActiveMQBytesMessage(); message.setContent(new ByteSequence(data)); for (int i = 0; i < 100; i++) { producer.send(message); messageCount.getAndIncrement(); } return new ArrayList<File>(FileUtils.listFiles(persistentDir, new WildcardFileFilter("*.log"), TrueFileFilter.INSTANCE)).size() > original; } })); // Consume all messages for (int i = 0; i < messageCount.get(); i++) { durable.receive(); } durable.close(); assertTrue("Subscription should go inactive", Wait.waitFor(new Condition() { @Override public boolean isSatisified() throws Exception { return broker.getAdminView().getInactiveDurableTopicSubscribers().length == 1; } })); // force a GC of unneeded journal files getBroker().getPersistenceAdapter().checkpoint(true); // wait until a journal file has been GC'd after receiving messages assertTrue("Should have garbage collected", Wait.waitFor(new Wait.Condition() { @Override public boolean isSatisified() throws Exception { return new ArrayList<File>(FileUtils.listFiles(persistentDir, new WildcardFileFilter("*.log"), TrueFileFilter.INSTANCE)).size() == original; } })); // stop the broker so we can blow away the index getBroker().stop(); getBroker().waitUntilStopped(); // delete the index so that the durables are gone from the index // The test passes if you take out this delete section for (File index : FileUtils.listFiles(persistentDir, new WildcardFileFilter("db.*"), TrueFileFilter.INSTANCE)) { FileUtils.deleteQuietly(index); } stopBroker(); setUpBroker(false); assertEquals(1, broker.getAdminView().getInactiveDurableTopicSubscribers().length); assertEquals(0, broker.getAdminView().getDurableTopicSubscribers().length); ActiveMQConnectionFactory connectionFactory2 = new ActiveMQConnectionFactory(this.brokerConnectURI); ActiveMQConnection connection2 = (ActiveMQConnection) connectionFactory2.createConnection(); connection2.setClientID("myId"); connection2.start(); final Session jmsSession2 = connection2.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE); TopicSubscriber durable2 = jmsSession2.createDurableSubscriber(new ActiveMQTopic("durable.sub"), "sub"); assertEquals(0, broker.getAdminView().getInactiveDurableTopicSubscribers().length); assertEquals(1, broker.getAdminView().getDurableTopicSubscribers().length); assertNull(durable2.receive(500)); }