本文整理匯總了Java中javax.jms.TopicSession.createConsumer方法的典型用法代碼示例。如果您正苦於以下問題:Java TopicSession.createConsumer方法的具體用法?Java TopicSession.createConsumer怎麽用?Java TopicSession.createConsumer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.jms.TopicSession
的用法示例。
在下文中一共展示了TopicSession.createConsumer方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: setUp
import javax.jms.TopicSession; //導入方法依賴的package包/類
@Before
public void setUp() throws Exception {
embeddedBroker.start();
out = new SimpleEventBus();
in = new SimpleEventBus();
ActiveMQConnectionFactory connectionFactory = embeddedBroker.createConnectionFactory();
topicConnection = connectionFactory.createTopicConnection();
topicConnection.start();
TopicSession topicSession = topicConnection.createTopicSession(
true, Session.SESSION_TRANSACTED);
TemporaryTopic topic = topicSession.createTemporaryTopic();
consumer = topicSession.createConsumer(topic);
publisher = new JmsPublisher(out);
publisher.setTopic(topic);
publisher.setConnectionFactory(connectionFactory);
publisher.postConstruct();
publisher.start();
jmsMessageSource = new JmsMessageSource(this.consumer,
new DefaultJmsMessageConverter(new XStreamSerializer()));
jmsMessageSource.subscribe(in::publish);
}
示例2: createSubscriber
import javax.jms.TopicSession; //導入方法依賴的package包/類
protected void createSubscriber(TopicSession topicSession, Topic topic) throws JMSException {
// Create subscriber
MessageConsumer subscriber = topicSession.createConsumer(topic);
// Attach message listener to subscriber
subscriber.setMessageListener(new MessageListener() {
public void onMessage(javax.jms.Message message) {
try {
// Process the message
processMessage(message);
} catch (Exception ex) {
// Error
logger.error("Error", ex);
}
}
});
}
示例3: testUnidentifiedProducer
import javax.jms.TopicSession; //導入方法依賴的package包/類
public void testUnidentifiedProducer() throws Exception
{
AMQConnection con = (AMQConnection) getConnection("guest", "guest");
AMQTopic topic = new AMQTopic(con,"MyTopic");
TopicSession session1 = con.createTopicSession(false, AMQSession.NO_ACKNOWLEDGE);
TopicPublisher publisher = session1.createPublisher(null);
MessageConsumer consumer1 = session1.createConsumer(topic);
con.start();
publisher.publish(topic, session1.createTextMessage("Hello"));
TextMessage m = (TextMessage) consumer1.receive(2000);
assertNotNull(m);
try
{
publisher.publish(session1.createTextMessage("Goodbye"));
fail("Did not throw UnsupportedOperationException");
}
catch (UnsupportedOperationException e)
{
// PASS
}
con.close();
}
示例4: testSendingSameMessage
import javax.jms.TopicSession; //導入方法依賴的package包/類
public void testSendingSameMessage() throws Exception
{
AMQConnection conn = (AMQConnection) getConnection("guest", "guest");
TopicSession session = conn.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
TemporaryTopic topic = session.createTemporaryTopic();
assertNotNull(topic);
TopicPublisher producer = session.createPublisher(topic);
MessageConsumer consumer = session.createConsumer(topic);
conn.start();
TextMessage sentMessage = session.createTextMessage("Test Message");
producer.send(sentMessage);
session.commit();
TextMessage receivedMessage = (TextMessage) consumer.receive(2000);
assertNotNull(receivedMessage);
assertEquals(sentMessage.getText(), receivedMessage.getText());
producer.send(sentMessage);
session.commit();
receivedMessage = (TextMessage) consumer.receive(2000);
assertNotNull(receivedMessage);
assertEquals(sentMessage.getText(), receivedMessage.getText());
session.commit();
conn.close();
}
示例5: testTextMessageCreation
import javax.jms.TopicSession; //導入方法依賴的package包/類
public void testTextMessageCreation() throws Exception
{
AMQConnection con = (AMQConnection) getConnection("guest", "guest");
AMQTopic topic = new AMQTopic(con, "MyTopic4");
TopicSession session1 = con.createTopicSession(true, AMQSession.AUTO_ACKNOWLEDGE);
TopicPublisher publisher = session1.createPublisher(topic);
MessageConsumer consumer1 = session1.createConsumer(topic);
con.start();
TextMessage tm = session1.createTextMessage("Hello");
publisher.publish(tm);
session1.commit();
tm = (TextMessage) consumer1.receive(10000L);
assertNotNull(tm);
String msgText = tm.getText();
assertEquals("Hello", msgText);
tm = session1.createTextMessage();
msgText = tm.getText();
assertNull(msgText);
publisher.publish(tm);
session1.commit();
tm = (TextMessage) consumer1.receive(10000L);
assertNotNull(tm);
session1.commit();
msgText = tm.getText();
assertNull(msgText);
tm.clearBody();
tm.setText("Now we are not null");
publisher.publish(tm);
session1.commit();
tm = (TextMessage) consumer1.receive(2000);
assertNotNull(tm);
session1.commit();
msgText = tm.getText();
assertEquals("Now we are not null", msgText);
tm = session1.createTextMessage("");
msgText = tm.getText();
assertEquals("Empty string not returned", "", msgText);
publisher.publish(tm);
session1.commit();
tm = (TextMessage) consumer1.receive(2000);
session1.commit();
assertNotNull(tm);
assertEquals("Empty string not returned", "", msgText);
con.close();
}
示例6: init
import javax.jms.TopicSession; //導入方法依賴的package包/類
@PostConstruct
@SuppressWarnings("unused")
private void init() throws Exception {
// Init Cache
Configuration c = new Configuration();
c.setName("sessionManager");
manager = CacheManager.create(c);
CacheConfiguration config = new CacheConfiguration();
config.eternal(false).name(CACHE_NAME).maxEntriesLocalHeap(maxEntries).memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LRU).timeToIdleSeconds(timeToIdle).timeToLiveSeconds(timeToLive);
if (!manager.cacheExists(CACHE_NAME)) {
manager.addCache(new Cache(config));
}
sessions = manager.getCache(CACHE_NAME);
// Init JMS replication
ConnectionFactory factory = new ActiveMQConnectionFactory(this.url);
Connection conn = factory.createConnection();
conn.start();
jmsSession = (TopicSession) conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Topic topic = jmsSession.createTopic(TOPIC_NAME);
tp = jmsSession.createPublisher(topic);
listener = new Thread() { // Thread created once upon container startup
@Override
public void run() {
try {
MessageConsumer consumer = jmsSession.createConsumer(topic);
while (live) {
ObjectMessage msg = (ObjectMessage) consumer.receive();
LOG.debug("Received replication message: {}", msg);
if (PUT.equals(msg.getStringProperty(ACTION_KEY))) {
sessions.put(new Element(msg.getStringProperty(TOKEN_KEY), msg.getObject()));
} else if (REMOVE.equals(msg.getStringProperty(ACTION_KEY))) {
sessions.remove(msg.getStringProperty(TOKEN_KEY));
}
}
} catch (JMSException e) {
LOG.error("Error reading replication message", e);
}
}
};
listener.start();
}