本文整理匯總了Java中javax.jms.TopicSession.createDurableSubscriber方法的典型用法代碼示例。如果您正苦於以下問題:Java TopicSession.createDurableSubscriber方法的具體用法?Java TopicSession.createDurableSubscriber怎麽用?Java TopicSession.createDurableSubscriber使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.jms.TopicSession
的用法示例。
在下文中一共展示了TopicSession.createDurableSubscriber方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testGetNoLocal
import javax.jms.TopicSession; //導入方法依賴的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) {}
}
示例2: testGetTopicSubscriber
import javax.jms.TopicSession; //導入方法依賴的package包/類
@Test
public void testGetTopicSubscriber() throws JMSException {
JmsPoolConnection connection = (JmsPoolConnection) cf.createTopicConnection();
TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTemporaryTopic();
JmsPoolTopicSubscriber subscriber = (JmsPoolTopicSubscriber) session.createDurableSubscriber(topic, "name", "color = red", true);
assertNotNull(subscriber.getTopicSubscriber());
assertTrue(subscriber.getTopicSubscriber() instanceof MockJMSTopicSubscriber);
subscriber.close();
try {
subscriber.getTopicSubscriber();
fail("Cannot read state on closed subscriber");
} catch (IllegalStateException ise) {}
}
示例3: collectMessagesFromDurableSubscriptionForOneMinute
import javax.jms.TopicSession; //導入方法依賴的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;
}
示例4: testMultipleDurableConsumersSendAndReceive
import javax.jms.TopicSession; //導入方法依賴的package包/類
@Test(timeout = 60000)
public void testMultipleDurableConsumersSendAndReceive() throws Exception {
Connection connection = createConnection("myClientId");
try {
TopicSession session = (TopicSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTopic(getTopicName());
int numMessages = 100;
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));
}
for (int i = 0; i < numMessages; 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);
}
} finally {
connection.close();
}
}
示例5: registerDurableSubscription
import javax.jms.TopicSession; //導入方法依賴的package包/類
private void registerDurableSubscription() throws JMSException {
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);
TopicSubscriber durableSubscriber = topicSession.createDurableSubscriber(topic, durableSubName);
connection.start();
durableSubscriber.close();
connection.close();
LOG.info("Durable Sub Registered");
}
示例6: MyMessageSubscriber
import javax.jms.TopicSession; //導入方法依賴的package包/類
public MyMessageSubscriber(Topic topic, String consumerName, int messageCount) throws JMSException, NamingException
{
_outstandingMsgCount = messageCount;
_topic=topic;
_consumerName = consumerName;
_connection = (TopicConnection) getConnection();
_session = (TopicSession) _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
_durSub = _session.createDurableSubscriber(_topic, _consumerName,"testprop='true'", false);
_connection.start();
}
示例7: testUnsubscriptionAfterConnectionClose
import javax.jms.TopicSession; //導入方法依賴的package包/類
public void testUnsubscriptionAfterConnectionClose() throws Exception
{
AMQConnection con1 = (AMQConnection) getClientConnection("guest", "guest", "clientid");
AMQTopic topic = new AMQTopic(con1, "MyTopic3");
TopicSession session1 = con1.createTopicSession(true, AMQSession.AUTO_ACKNOWLEDGE);
TopicPublisher publisher = session1.createPublisher(topic);
AMQConnection con2 = (AMQConnection) getClientConnection("guest", "guest", "clientid");
TopicSession session2 = con2.createTopicSession(true, AMQSession.AUTO_ACKNOWLEDGE);
TopicSubscriber sub = session2.createDurableSubscriber(topic, "subscription0");
con2.start();
publisher.publish(session1.createTextMessage("Hello"));
session1.commit();
TextMessage tm = (TextMessage) sub.receive(2000);
session2.commit();
assertNotNull(tm);
con2.close();
publisher.publish(session1.createTextMessage("Hello2"));
session1.commit();
con2 = (AMQConnection) getClientConnection("guest", "guest", "clientid");
session2 = con2.createTopicSession(true, AMQSession.NO_ACKNOWLEDGE);
sub = session2.createDurableSubscriber(topic, "subscription0");
con2.start();
tm = (TextMessage) sub.receive(2000);
session2.commit();
assertNotNull(tm);
assertEquals("Hello2", tm.getText());
session2.unsubscribe("subscription0");
con1.close();
con2.close();
}
示例8: subscribeTopic
import javax.jms.TopicSession; //導入方法依賴的package包/類
public static TopicSubscriber subscribeTopic(TopicSession session, String topicName, String subscriberName, boolean durable, MessageListener listener)
throws JMSException {
TopicSubscriber ts = null;
if (durable)
ts = session.createDurableSubscriber(session.createTopic(topicName), subscriberName);
else
ts = session.createSubscriber(session.createTopic(topicName));
ts.setMessageListener(listener);
session.commit();
return ts;
}
示例9: getTopicSubscriber
import javax.jms.TopicSession; //導入方法依賴的package包/類
private TopicSubscriber getTopicSubscriber(TopicSession session, Topic topic, String selector) throws NamingException, JMSException {
TopicSubscriber topicSubscriber;
if (subscriberType.equalsIgnoreCase("DURABLE")) {
topicSubscriber =
session.createDurableSubscriber(topic, destinationName, selector, false);
if (log.isDebugEnabled()) log.debug("[" + name + "] got durable subscriber for topic [" + destinationName + "] with selector [" + selector + "]");
} else {
topicSubscriber = session.createSubscriber(topic, selector, false);
if (log.isDebugEnabled()) log.debug("[" + name + "] got transient subscriber for topic [" + destinationName + "] with selector [" + selector + "]");
}
return topicSubscriber;
}
示例10: testTopicNoLocalDurable
import javax.jms.TopicSession; //導入方法依賴的package包/類
@Test
public void testTopicNoLocalDurable() throws Exception {
connection.setClientID("forNoLocal-1");
connection.start();
TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
System.out.println("creating queue: " + topicName);
Topic dest = new ActiveMQTopic(topicName);
MessageConsumer nolocalConsumer = session.createDurableSubscriber(dest, "nolocal-subscriber1", "", true);
MessageConsumer consumer = session.createDurableSubscriber(dest, "normal-subscriber", null, false);
MessageConsumer selectorConsumer = session.createDurableSubscriber(dest, "selector-subscriber", "TESTKEY = 'test'", false);
MessageProducer producer = session.createProducer(dest);
final String body1 = "MfromAMQ-1";
final String body2 = "MfromAMQ-2";
TextMessage msg = session.createTextMessage(body1);
producer.send(msg);
msg = session.createTextMessage(body2);
msg.setStringProperty("TESTKEY", "test");
producer.send(msg);
//receive nolocal
TextMessage receivedMsg = (TextMessage) nolocalConsumer.receive(1000);
assertNull("nolocal consumer got: " + receivedMsg, receivedMsg);
//receive normal consumer
receivedMsg = (TextMessage) consumer.receive(1000);
assertNotNull(receivedMsg);
assertEquals(body1, receivedMsg.getText());
receivedMsg = (TextMessage) consumer.receive(1000);
assertNotNull(receivedMsg);
assertEquals(body2, receivedMsg.getText());
assertNull(consumer.receiveNoWait());
//selector should only receive one
receivedMsg = (TextMessage) selectorConsumer.receive(1000);
assertNotNull(receivedMsg);
assertEquals(body2, receivedMsg.getText());
assertEquals("test", receivedMsg.getStringProperty("TESTKEY"));
assertNull(selectorConsumer.receiveNoWait());
//send from another connection
Connection anotherConn = this.factory.createConnection();
try {
anotherConn.start();
Session anotherSession = anotherConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer anotherProducer = anotherSession.createProducer(dest);
TextMessage anotherMsg = anotherSession.createTextMessage(body1);
anotherProducer.send(anotherMsg);
assertNotNull(consumer.receive(1000));
assertNull(selectorConsumer.receive(1000));
assertNotNull(nolocalConsumer.receive(1000));
} finally {
anotherConn.close();
}
session.close();
}
示例11: testDurSubRestoredAfterNonPersistentMessageSent
import javax.jms.TopicSession; //導入方法依賴的package包/類
/**
* test strategy:
* create and register a durable subscriber then close it
* create a publisher and send a persistant message followed by a non persistant message
* crash and restart the broker
* recreate the durable subscriber and check that only the first message is received
*/
public void testDurSubRestoredAfterNonPersistentMessageSent() throws Exception
{
if (isBrokerStorePersistent())
{
TopicConnectionFactory factory = getConnectionFactory();
Topic topic = (Topic) getInitialContext().lookup(_topicName);
//create and register a durable subscriber then close it
TopicConnection durConnection = factory.createTopicConnection("guest", "guest");
TopicSession durSession = durConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber durSub1 = durSession.createDurableSubscriber(topic, "dursub");
durConnection.start();
durSub1.close();
durSession.close();
durConnection.stop();
//create a publisher and send a persistant message followed by a non persistant message
TopicConnection pubConnection = factory.createTopicConnection("guest", "guest");
TopicSession pubSession = pubConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicPublisher publisher = pubSession.createPublisher(topic);
Message message = pubSession.createMessage();
message.setIntProperty("count", 1);
publisher.publish(message, javax.jms.DeliveryMode.PERSISTENT, javax.jms.Message.DEFAULT_PRIORITY,
javax.jms.Message.DEFAULT_TIME_TO_LIVE);
message.setIntProperty("count", 2);
publisher.publish(message, javax.jms.DeliveryMode.NON_PERSISTENT, javax.jms.Message.DEFAULT_PRIORITY,
javax.jms.Message.DEFAULT_TIME_TO_LIVE);
publisher.close();
pubSession.close();
//now stop the server
try
{
restartBroker();
}
catch (Exception e)
{
_logger.error("problems restarting broker: " + e);
throw e;
}
//now recreate the durable subscriber and check the received messages
factory = getConnectionFactory();
topic = (Topic) getInitialContext().lookup(_topicName);
TopicConnection durConnection2 = factory.createTopicConnection("guest", "guest");
TopicSession durSession2 = durConnection2.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicSubscriber durSub2 = durSession2.createDurableSubscriber(topic, "dursub");
durConnection2.start();
Message m1 = durSub2.receive(1000);
if (m1 == null)
{
assertTrue("testDurSubRestoredAfterNonPersistentMessageSent test failed. no message was returned",
false);
}
assertTrue("testDurSubRestoredAfterNonPersistentMessageSent test failed. Wrong message was returned.",
m1.getIntProperty("count") == 1);
durSession2.unsubscribe("dursub");
durConnection2.close();
}
}
示例12: subscriptionNameReuseForDifferentTopic
import javax.jms.TopicSession; //導入方法依賴的package包/類
private void subscriptionNameReuseForDifferentTopic(boolean shutdown) throws Exception
{
AMQConnection con = (AMQConnection) getConnection("guest", "guest");
AMQTopic topic = new AMQTopic(con, "MyTopic1" + String.valueOf(shutdown));
AMQTopic topic2 = new AMQTopic(con, "MyOtherTopic1" + String.valueOf(shutdown));
TopicSession session1 = con.createTopicSession(true, AMQSession.AUTO_ACKNOWLEDGE);
TopicSubscriber sub = session1.createDurableSubscriber(topic, "subscription0");
TopicPublisher publisher = session1.createPublisher(null);
con.start();
publisher.publish(topic, session1.createTextMessage("hello"));
session1.commit();
TextMessage m = (TextMessage) sub.receive(2000);
assertNotNull(m);
session1.commit();
if (shutdown)
{
session1.close();
con.close();
con = (AMQConnection) getConnection("guest", "guest");
con.start();
session1 = con.createTopicSession(true, AMQSession.NO_ACKNOWLEDGE);
publisher = session1.createPublisher(null);
}
sub.close();
TopicSubscriber sub2 = session1.createDurableSubscriber(topic2, "subscription0");
publisher.publish(topic, session1.createTextMessage("hello"));
session1.commit();
if (!shutdown)
{
m = (TextMessage) sub2.receive(2000);
assertNull(m);
session1.commit();
}
publisher.publish(topic2, session1.createTextMessage("goodbye"));
session1.commit();
m = (TextMessage) sub2.receive(2000);
assertNotNull(m);
assertEquals("goodbye", m.getText());
session1.unsubscribe("subscription0");
con.close();
}
示例13: subscribe
import javax.jms.TopicSession; //導入方法依賴的package包/類
/**
* {@inheritDoc}
*/
public void subscribe(Subscription subscription) throws EventBrokerException {
if (isDeactivated()) {
return;
}
// in a multi tenant environment deployment synchronize may creates subscriptions before
// the event observer get activated.
if (this.subscriptionIDSessionDetailsMap.containsKey(subscription.getId())) {
log.warn("There is an subscription already exists for the subscription with id " + subscription.getId());
return;
}
JMSMessageListener jmsMessageListener =
new JMSMessageListener(this.notificationManager, subscription);
try {
TopicConnection topicConnection = getTopicConnection(subscription.getOwner());
TopicSession topicSession =
topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
String topicName = "";
if (subscription.getTenantDomain() != null && (!subscription.getTenantDomain().equals(org.wso2.carbon.base.
MultitenantConstants.SUPER_TENANT_DOMAIN_NAME))) {
if (!subscription.getTopicName().startsWith("/")) {
topicName = getTopicName(subscription.getTenantDomain() + "/" + subscription.getTopicName());
} else {
topicName = getTopicName(subscription.getTenantDomain() + subscription.getTopicName());
}
} else {
topicName = getTopicName(subscription.getTopicName());
}
Topic topic = topicSession.createTopic(topicName);
//Some times we are not getting the proper topic with the required syntax, if it is not
//appropriate we need to check and add the BURL syntax to fix the issue https://wso2.org/jira/browse/MB-185
if (!topic.toString().startsWith("topic://amq.topic")) {
topic = topicSession.createTopic("BURL:" + topicName);
}
TopicSubscriber topicSubscriber =
topicSession.createDurableSubscriber(topic, subscription.getId());
topicSubscriber.setMessageListener(jmsMessageListener);
this.subscriptionIDSessionDetailsMap.put(subscription.getId(),
new JMSSubscriptionDetails(topicSubscriber, topicSession, topicConnection));
} catch (JMSException e) {
throw new EventBrokerException("Can not subscribe to topic " + subscription.getTopicName() + " " + e.getMessage(), e);
}
}