本文整理匯總了Java中javax.jms.TopicSession類的典型用法代碼示例。如果您正苦於以下問題:Java TopicSession類的具體用法?Java TopicSession怎麽用?Java TopicSession使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
TopicSession類屬於javax.jms包,在下文中一共展示了TopicSession類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testWithSessionCloseOutsideTheLoop
import javax.jms.TopicSession; //導入依賴的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: doTestCreateTopicPublisher
import javax.jms.TopicSession; //導入依賴的package包/類
private void doTestCreateTopicPublisher(boolean useAnonymousProducers) throws JMSException {
cf.setUseAnonymousProducers(useAnonymousProducers);
JmsPoolConnection connection = (JmsPoolConnection) cf.createConnection();
TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic1 = session.createTopic("Topic-1");
Topic topic2 = session.createTopic("Topic-2");
JmsPoolTopicPublisher publisher1 = (JmsPoolTopicPublisher) session.createPublisher(topic1);
JmsPoolTopicPublisher publisher2 = (JmsPoolTopicPublisher) session.createPublisher(topic2);
if (useAnonymousProducers) {
assertSame(publisher1.getMessageProducer(), publisher2.getMessageProducer());
} else {
assertNotSame(publisher1.getMessageProducer(), publisher2.getMessageProducer());
}
connection.close();
}
示例3: testGetTopic
import javax.jms.TopicSession; //導入依賴的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) {}
}
示例4: 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) {}
}
示例5: 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) {}
}
示例6: testGetTopic
import javax.jms.TopicSession; //導入依賴的package包/類
@Test
public void testGetTopic() throws JMSException {
JmsPoolConnection connection = (JmsPoolConnection) cf.createTopicConnection();
TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTemporaryTopic();
TopicPublisher publisher = session.createPublisher(topic);
assertNotNull(publisher.getTopic());
assertSame(topic, publisher.getTopic());
publisher.close();
try {
publisher.getTopic();
fail("Cannot read topic on closed publisher");
} catch (IllegalStateException ise) {}
}
示例7: testGetTopicPublisher
import javax.jms.TopicSession; //導入依賴的package包/類
@Test
public void testGetTopicPublisher() throws JMSException {
JmsPoolConnection connection = (JmsPoolConnection) cf.createTopicConnection();
TopicSession session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
Topic topic = session.createTemporaryTopic();
JmsPoolTopicPublisher publisher = (JmsPoolTopicPublisher) session.createPublisher(topic);
assertNotNull(publisher.getTopicPublisher());
assertTrue(publisher.getTopicPublisher() instanceof MockJMSTopicPublisher);
publisher.close();
try {
publisher.getTopicPublisher();
fail("Cannot read state on closed publisher");
} catch (IllegalStateException ise) {}
}
示例8: testSenderAndPublisherDest
import javax.jms.TopicSession; //導入依賴的package包/類
@Test(timeout = 60000)
public void testSenderAndPublisherDest() throws Exception {
JmsPoolXAConnectionFactory pcf = new JmsPoolXAConnectionFactory();
pcf.setConnectionFactory(new ActiveMQXAConnectionFactory(
"vm://test?broker.persistent=false&broker.useJmx=false"));
QueueConnection connection = pcf.createQueueConnection();
QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
QueueSender sender = session.createSender(session.createQueue("AA"));
assertNotNull(sender.getQueue().getQueueName());
connection.close();
TopicConnection topicConnection = pcf.createTopicConnection();
TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
TopicPublisher topicPublisher = topicSession.createPublisher(topicSession.createTopic("AA"));
assertNotNull(topicPublisher.getTopic().getTopicName());
topicConnection.close();
pcf.stop();
}
示例9: publish
import javax.jms.TopicSession; //導入依賴的package包/類
/**
* @param topicConnection
* @param chatTopic
* @param userId
* @throws JMSException
* @throws IOException
*/
void publish(TopicConnection topicConnection, Topic chatTopic, String userId)
throws JMSException, IOException {
TopicSession tsession = topicConnection.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
TopicPublisher topicPublisher = tsession.createPublisher(chatTopic);
topicConnection.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
while (true) {
String msgToSend = reader.readLine();
if (msgToSend.equalsIgnoreCase("exit")) {
topicConnection.close();
System.exit(0);
} else {
TextMessage msg = (TextMessage) tsession.createTextMessage();
msg.setText("\n["+userId + " : " + msgToSend+"]");
topicPublisher.publish(msg);
}
}
}
示例10: create
import javax.jms.TopicSession; //導入依賴的package包/類
public static ManagedSession create( Session session ) {
if ( (session instanceof XAQueueSession) && (session instanceof XATopicSession))
return new ManagedXAQueueTopicSession(session);
if (session instanceof XAQueueSession)
return new ManagedXAQueueSession((XAQueueSession) session);
if (session instanceof XATopicSession)
return new ManagedXATopicSession((XATopicSession) session);
if ( (session instanceof QueueSession) && (session instanceof TopicSession))
return new ManagedQueueTopicSession(session);
if (session instanceof QueueSession)
return new ManagedQueueSession((QueueSession) session);
if (session instanceof TopicSession)
return new ManagedTopicSession((TopicSession) session);
return new ManagedSession(session);
}
示例11: send
import javax.jms.TopicSession; //導入依賴的package包/類
/**
* Sends the given {@code events} to the configured JMS Topic. It takes the current Unit of Work
* into account when available. Otherwise, it simply publishes directly.
*
* @param events the events to publish on the JMS Message Broker
*/
protected void send(List<? extends EventMessage<?>> events) {
try (TopicConnection topicConnection = connectionFactory.createTopicConnection()) {
int ackMode = isTransacted ? Session.SESSION_TRANSACTED : Session.AUTO_ACKNOWLEDGE;
TopicSession topicSession = topicConnection.createTopicSession(isTransacted, ackMode);
try (TopicPublisher publisher = topicSession.createPublisher(topic)) {
for (EventMessage event : events) {
Message jmsMessage = messageConverter.createJmsMessage(event, topicSession);
doSendMessage(publisher, jmsMessage);
}
} finally {
handleTransaction(topicSession);
}
} catch (JMSException ex) {
throw new EventPublicationFailedException(
"Unable to establish TopicConnection to JMS message broker.", ex);
}
}
示例12: 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);
}
示例13: testSendMessage_NoUnitOfWork
import javax.jms.TopicSession; //導入依賴的package包/類
@Test
public void testSendMessage_NoUnitOfWork() throws Exception {
TopicConnection connection = mock(TopicConnection.class);
when(connectionFactory.createTopicConnection()).thenReturn(connection);
TopicSession transactionalSession = mock(TopicSession.class);
when(connection.createTopicSession(true, Session.SESSION_TRANSACTED))
.thenReturn(transactionalSession);
when(transactionalSession.createPublisher(topic)).thenReturn(publisher);
GenericEventMessage<String> message = new GenericEventMessage<>("Message");
TextMessage jmsMessage = mock(TextMessage.class);
when(converter.createJmsMessage(message, transactionalSession)).thenReturn(jmsMessage);
eventBus.publish(message);
verify(publisher).publish(jmsMessage);
verify(transactionalSession).commit();
verify(transactionalSession).close();
}
示例14: testSendMessage_WithTransactionalUnitOfWork
import javax.jms.TopicSession; //導入依賴的package包/類
@Test
public void testSendMessage_WithTransactionalUnitOfWork() throws Exception {
GenericEventMessage<String> message = new GenericEventMessage<>("Message");
final UnitOfWork<?> uow = DefaultUnitOfWork.startAndGet(message);
TopicConnection connection = mock(TopicConnection.class);
when(connectionFactory.createTopicConnection()).thenReturn(connection);
TopicSession transactionalSession = mock(TopicSession.class);
when(connection.createTopicSession(true, Session.SESSION_TRANSACTED))
.thenReturn(transactionalSession);
when(transactionalSession.createPublisher(topic)).thenReturn(publisher);
when(transactionalSession.getTransacted()).thenReturn(true);
TextMessage jmsMessage = mock(TextMessage.class);
when(converter.createJmsMessage(message, transactionalSession)).thenReturn(jmsMessage);
eventBus.publish(message);
uow.commit();
verify(publisher).publish(jmsMessage);
verify(transactionalSession).commit();
verify(transactionalSession).close();
}
示例15: testSendMessage_WithUnitOfWorkRollback
import javax.jms.TopicSession; //導入依賴的package包/類
@Test
public void testSendMessage_WithUnitOfWorkRollback() throws Exception {
GenericEventMessage<String> message = new GenericEventMessage<>("Message");
final UnitOfWork<?> uow = DefaultUnitOfWork.startAndGet(message);
TopicConnection connection = mock(TopicConnection.class);
when(connectionFactory.createTopicConnection()).thenReturn(connection);
TopicSession transactionalSession = mock(TopicSession.class);
when(connection.createTopicSession(true, Session.SESSION_TRANSACTED))
.thenReturn(transactionalSession);
when(transactionalSession.createPublisher(topic)).thenReturn(publisher);
when(transactionalSession.getTransacted()).thenReturn(true);
TextMessage jmsMessage = mock(TextMessage.class);
when(converter.createJmsMessage(message, transactionalSession)).thenReturn(jmsMessage);
eventBus.publish(message);
verify(transactionalSession, never()).rollback();
verify(transactionalSession, never()).commit();
verify(transactionalSession, never()).close();
uow.rollback();
verify(publisher, never()).publish(jmsMessage);
verify(transactionalSession, never()).commit();
verify(connectionFactory, never()).createTopicConnection();
}