本文整理匯總了Java中javax.jms.TopicSubscriber類的典型用法代碼示例。如果您正苦於以下問題:Java TopicSubscriber類的具體用法?Java TopicSubscriber怎麽用?Java TopicSubscriber使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
TopicSubscriber類屬於javax.jms包,在下文中一共展示了TopicSubscriber類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testWithSessionCloseOutsideTheLoop
import javax.jms.TopicSubscriber; //導入依賴的package包/類
public void testWithSessionCloseOutsideTheLoop() throws Exception {
TopicConnection connection = connectionFactory.createTopicConnection();
connection.start();
TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
for (int i = 0; i < 100; i++) {
TopicSubscriber subscriber = subscriberSession.createSubscriber(topic);
DummyMessageListener listener = new DummyMessageListener();
subscriber.setMessageListener(listener);
subscriber.close();
}
subscriberSession.close();
connection.close();
Thread.sleep(1000);
Destination dest = backEnd.getRegionBroker().getDestinationMap().get(topic);
assertNotNull(dest);
assertTrue(dest.getConsumers().isEmpty());
}
示例2: testGetTopic
import javax.jms.TopicSubscriber; //導入依賴的package包/類
@Test
public void testGetTopic() throws JMSException {
JmsPoolConnection connection = (JmsPoolConnection) cf.createTopicConnection();
TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTemporaryTopic();
TopicSubscriber subscriber = session.createSubscriber(topic);
assertNotNull(subscriber.getTopic());
assertSame(topic, subscriber.getTopic());
subscriber.close();
try {
subscriber.getTopic();
fail("Cannot read topic on closed subscriber");
} catch (IllegalStateException ise) {}
}
示例3: testGetNoLocal
import javax.jms.TopicSubscriber; //導入依賴的package包/類
@Test
public void testGetNoLocal() throws JMSException {
JmsPoolConnection connection = (JmsPoolConnection) cf.createTopicConnection();
TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTemporaryTopic();
TopicSubscriber subscriber = session.createDurableSubscriber(topic, "name", "color = red", true);
assertTrue(subscriber.getNoLocal());
subscriber.close();
try {
subscriber.getNoLocal();
fail("Cannot read state on closed subscriber");
} catch (IllegalStateException ise) {}
}
示例4: JMSSink
import javax.jms.TopicSubscriber; //導入依賴的package包/類
public JMSSink(final String tcfBindingName, final String topicBindingName, final String username,
final String password) {
try {
final Context ctx = new InitialContext();
final TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) lookup(ctx,
tcfBindingName);
final TopicConnection topicConnection =
topicConnectionFactory.createTopicConnection(username,
password);
topicConnection.start();
final TopicSession topicSession = topicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
final Topic topic = (Topic) ctx.lookup(topicBindingName);
final TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
topicSubscriber.setMessageListener(this);
} catch (final Exception e) {
logger.error("Could not read JMS message.", e);
}
}
示例5: closeConsumer
import javax.jms.TopicSubscriber; //導入依賴的package包/類
/**
* Close a JMS {@link MessageConsumer}.
* @param messageConsumer JMS Message Consumer that needs to be closed.
* @throws JMSException if an error occurs while closing the consumer.
*/
public void closeConsumer(MessageConsumer messageConsumer) throws JMSException {
if (messageConsumer != null) {
if (logger.isDebugEnabled()) {
logger.debug("Closing a JMS Message Consumer of: " + this.connectionFactoryString);
}
if ((JMSConstants.JMS_SPEC_VERSION_1_1.equals(jmsSpec)) || (JMSConstants.JMS_SPEC_VERSION_2_0
.equals(jmsSpec))) {
messageConsumer.close();
} else {
if (JMSConstants.JMSDestinationType.QUEUE.equals(this.destinationType)) {
if (messageConsumer instanceof QueueReceiver) {
((QueueReceiver) messageConsumer).close();
}
} else {
if (messageConsumer instanceof TopicSubscriber) {
((TopicSubscriber) messageConsumer).close();
}
}
}
}
}
示例6: testNotificationProperties
import javax.jms.TopicSubscriber; //導入依賴的package包/類
@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"));
}
}
}
示例7: testSendAndReceiveOnTopic
import javax.jms.TopicSubscriber; //導入依賴的package包/類
@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();
}
}
示例8: testDurableSubscriptionUnsubscribe
import javax.jms.TopicSubscriber; //導入依賴的package包/類
@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();
}
}
示例9: testWithSessionAndSubsciberClose
import javax.jms.TopicSubscriber; //導入依賴的package包/類
public void testWithSessionAndSubsciberClose() throws Exception {
TopicConnection connection = connectionFactory.createTopicConnection();
connection.start();
for (int i = 0; i < 100; i++) {
TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber subscriber = subscriberSession.createSubscriber(topic);
DummyMessageListener listener = new DummyMessageListener();
subscriber.setMessageListener(listener);
subscriber.close();
subscriberSession.close();
}
connection.close();
Thread.sleep(1000);
Destination dest = backEnd.getRegionBroker().getDestinationMap().get(topic);
assertNotNull(dest);
assertTrue(dest.getConsumers().isEmpty());
}
示例10: testWithOneSubscriber
import javax.jms.TopicSubscriber; //導入依賴的package包/類
public void testWithOneSubscriber() throws Exception {
TopicConnection connection = connectionFactory.createTopicConnection();
connection.start();
TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber subscriber = subscriberSession.createSubscriber(topic);
DummyMessageListener listener = new DummyMessageListener();
subscriber.setMessageListener(listener);
subscriber.close();
subscriberSession.close();
connection.close();
Thread.sleep(1000);
Destination dest = backEnd.getRegionBroker().getDestinationMap().get(topic);
assertNotNull(dest);
assertTrue(dest.getConsumers().isEmpty());
}
示例11: testWithoutSessionAndSubsciberClose
import javax.jms.TopicSubscriber; //導入依賴的package包/類
public void testWithoutSessionAndSubsciberClose() throws Exception {
TopicConnection connection = connectionFactory.createTopicConnection();
connection.start();
for (int i = 0; i < 100; i++) {
TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber subscriber = subscriberSession.createSubscriber(topic);
assertNotNull(subscriber);
}
connection.close();
Thread.sleep(1000);
Destination dest = backEnd.getRegionBroker().getDestinationMap().get(topic);
assertNotNull(dest);
assertTrue(dest.getConsumers().isEmpty());
}
示例12: testWithoutSessionAndSubsciberClosePlayAround
import javax.jms.TopicSubscriber; //導入依賴的package包/類
/**
* Running this test you can produce a leak of only 2 ConsumerInfo on BE
* broker, NOT 200 as in other cases!
*/
public void testWithoutSessionAndSubsciberClosePlayAround() throws Exception {
TopicConnection connection = connectionFactory.createTopicConnection();
connection.start();
for (int i = 0; i < 100; i++) {
TopicSession subscriberSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber subscriber = subscriberSession.createSubscriber(topic);
DummyMessageListener listener = new DummyMessageListener();
subscriber.setMessageListener(listener);
if (i != 50) {
subscriber.close();
subscriberSession.close();
}
}
connection.close();
Thread.sleep(1000);
Destination dest = backEnd.getRegionBroker().getDestinationMap().get(topic);
assertNotNull(dest);
assertTrue(dest.getConsumers().isEmpty());
}
示例13: collectMessagesFromDurableSubscriptionForOneMinute
import javax.jms.TopicSubscriber; //導入依賴的package包/類
private Message collectMessagesFromDurableSubscriptionForOneMinute() throws Exception {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://" + brokerName);
TopicConnection connection = connectionFactory.createTopicConnection();
connection.setClientID(clientID);
TopicSession topicSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = topicSession.createTopic(topicName);
connection.start();
TopicSubscriber subscriber = topicSession.createDurableSubscriber(topic, durableSubName);
LOG.info("About to receive messages");
Message message = subscriber.receive(120000);
subscriber.close();
connection.close();
LOG.info("collectMessagesFromDurableSubscriptionForOneMinute done");
return message;
}
示例14: testRemove
import javax.jms.TopicSubscriber; //導入依賴的package包/類
public void testRemove() throws Exception {
Connection connection = createConnection();
connection.setClientID("cliID");
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber subscriber = session.createDurableSubscriber((Topic) createDestination(), "subName");
subscriber.close();
connection.close();
assertTrue(Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
return broker.getAdminView().getInactiveDurableTopicSubscribers().length == 0;
}
}, 15000));
}
示例15: testRemoveAfterRestart
import javax.jms.TopicSubscriber; //導入依賴的package包/類
public void testRemoveAfterRestart() throws Exception {
Connection connection = createConnection();
connection.setClientID("cliID");
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber subscriber = session.createDurableSubscriber((Topic) createDestination(), "subName");
subscriber.close();
connection.close();
LOG.info("Broker restarting, wait for inactive cleanup afterwards.");
restartBroker();
LOG.info("Broker restarted, wait for inactive cleanup now.");
assertTrue(broker.getAdminView().getInactiveDurableTopicSubscribers().length == 1);
assertTrue(Wait.waitFor(new Wait.Condition() {
@Override
public boolean isSatisified() throws Exception {
return broker.getAdminView().getInactiveDurableTopicSubscribers().length == 0;
}
}, 20000));
}