use of javax.jms.TopicSession in project spring-framework by spring-projects.
the class SingleConnectionFactoryTests method testCachingConnectionFactoryWithTopicConnectionFactoryAndJms102Usage.
@Test
public void testCachingConnectionFactoryWithTopicConnectionFactoryAndJms102Usage() throws JMSException {
TopicConnectionFactory cf = mock(TopicConnectionFactory.class);
TopicConnection con = mock(TopicConnection.class);
TopicSession txSession = mock(TopicSession.class);
TopicSession nonTxSession = mock(TopicSession.class);
given(cf.createTopicConnection()).willReturn(con);
given(con.createTopicSession(true, Session.AUTO_ACKNOWLEDGE)).willReturn(txSession);
given(txSession.getTransacted()).willReturn(true);
given(con.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE)).willReturn(nonTxSession);
CachingConnectionFactory scf = new CachingConnectionFactory(cf);
scf.setReconnectOnException(false);
Connection con1 = scf.createTopicConnection();
Session session1 = con1.createSession(true, Session.AUTO_ACKNOWLEDGE);
session1.getTransacted();
// should lead to rollback
session1.close();
session1 = con1.createSession(false, Session.CLIENT_ACKNOWLEDGE);
session1.close();
con1.start();
TopicConnection con2 = scf.createTopicConnection();
Session session2 = con2.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);
session2.close();
session2 = con2.createSession(true, Session.AUTO_ACKNOWLEDGE);
session2.getTransacted();
session2.close();
con2.start();
con1.close();
con2.close();
// should trigger actual close
scf.destroy();
verify(txSession).close();
verify(nonTxSession).close();
verify(con).start();
verify(con).stop();
verify(con).close();
}
use of javax.jms.TopicSession in project spring-framework by spring-projects.
the class DynamicDestinationResolverTests method resolveWithPubSubTopicSession.
@Test
public void resolveWithPubSubTopicSession() throws Exception {
Topic expectedDestination = new StubTopic();
TopicSession session = mock(TopicSession.class);
given(session.createTopic(DESTINATION_NAME)).willReturn(expectedDestination);
testResolveDestination(session, expectedDestination, true);
}
use of javax.jms.TopicSession in project ACS by ACS-Community.
the class PubSubTest method testTopicConnection.
public void testTopicConnection() throws Exception {
TopicConnection topicConnection = factory.createTopicConnection();
assertNotNull("Error creating the TopicConnection", topicConnection);
TopicSession topicSession = topicConnection.createTopicSession(true, 0);
assertNotNull("Error creating the TopicSession", topicSession);
String chName = "TOPIC.TEST";
Topic topic = topicSession.createTopic(chName);
assertNotNull("Error creating Topic", topic);
assertEquals("Wrong name for the topic", chName, topic.getTopicName());
TopicPublisher publisher = topicSession.createPublisher(topic);
assertNotNull("Error creating the TopicPublisher ", publisher);
TopicSubscriber subscriber = topicSession.createSubscriber(topic);
assertNotNull("Error creating the TopicSubscriber", subscriber);
subscriber.setMessageListener(this);
ACSJMSTextMessage msg = new ACSJMSTextMessage(getContainerServices());
assertNotNull("Error creating ACSJMSTextMessage", msg);
msg.setText(msgToSend);
publisher.publish(msg);
// Wait for awhile to have enough time to receive the message
try {
Thread.sleep(10000);
} catch (Exception e) {
}
publisher.close();
subscriber.close();
}
use of javax.jms.TopicSession in project ACS by ACS-Community.
the class DefaultSubscriberImpl method subscribe.
/**
* Method subscribe
*
*
* @param topic
* @param listener
* @param selector
*
* @return long the subscription handle identifier
*
* @throws JMSException
* @throws NamingException
*
*/
public long subscribe(String topic, SubscriptionListener listener, String selector) throws JMSException, NamingException {
cat.info("subscribe(" + topic + ", listener, " + selector + ")");
if (closed) {
throw (new JMSException("Subscriber closed."));
}
SubscriptionHandle handle = new SubscriptionHandle();
handle.setSubscriptionTopic(topic);
handle.setSubscriptionSelector(selector);
handle.setSubscriptionListener(listener);
StringBuffer local_selector = new StringBuffer();
if (NotificationHelper.isNotification(topic)) {
// this is a subscription to notifications, no further selection is required
local_selector.append(selector);
} else {
// subscription to a generic topic, adding subscriber specific selection
if (selector != null) {
local_selector.append(selector);
local_selector.append(" AND ");
}
local_selector.append("( (");
local_selector.append(NotificationHelper.SUBSCRIPTION_ID_PROPERTY);
local_selector.append(" IS NULL) OR (");
local_selector.append(NotificationHelper.SUBSCRIPTION_ID_PROPERTY);
local_selector.append(" = '");
local_selector.append(subscriberId);
local_selector.append("@");
local_selector.append(handle.getSubscriptionToken());
local_selector.append("') )");
}
TopicSession session = null;
TopicSubscriber subscriber = null;
Topic the_topic = createTopic(topic);
try {
session = connection.createTopicSession();
subscriber = session.createSubscriber(the_topic, local_selector.toString(), false);
} catch (JMSSecurityException jse) {
cat.error("JMSSecurityException caught");
throw (new NamingException(jse.getMessage()));
} catch (JMSException je) {
cat.error("JMSException caught");
throw (new NamingException(je.getMessage()));
} catch (ConnectionException ce) {
cat.error("ConnectionException caught");
throw (new JMSException(ce.getMessage()));
} catch (Exception e) {
cat.error("Generic exception caught", e);
}
subscriber.setMessageListener(listener);
handle.setSubscriber(subscriber);
handle.setSession(session);
synchronized (subscribers) {
subscribers.put(new Long(handle.getSubscriptionToken()), handle);
}
if (!NotificationHelper.isNotification(topic)) {
publishNotification(NotificationHelper.CONSUMER_OPEN_NOTIFICATION, handle);
}
return handle.getSubscriptionToken();
}
use of javax.jms.TopicSession in project ACS by ACS-Community.
the class DefaultSubscriberImpl method recoverSubscriptions.
/**
* Method recoverSubscriptions
*
*/
protected void recoverSubscriptions() {
cat.info("recoverSubscriptions()");
try {
SubscriptionHandle handle = null;
Iterator iterator = subscribers.values().iterator();
while (iterator.hasNext()) {
handle = (SubscriptionHandle) iterator.next();
if (!NotificationHelper.isNotification(handle.getSubscriptionTopic())) {
publishNotification(NotificationHelper.CONSUMER_CLOSE_NOTIFICATION, handle);
}
cat.debug("Recovering subscription to : " + handle.getSubscriptionTopic());
TopicSession session = connection.createTopicSession();
TopicSubscriber subscriber = session.createSubscriber(createTopic(handle.getSubscriptionTopic()), handle.getSubscriptionSelector(), false);
subscriber.setMessageListener(handle.getSubscriptionListener());
handle.setSession(session);
handle.setSubscriber(subscriber);
if (!NotificationHelper.isNotification(handle.getSubscriptionTopic())) {
publishNotification(NotificationHelper.CONSUMER_OPEN_NOTIFICATION, handle);
}
}
cat.info("Subscriptions succesfully recovered.");
} catch (Exception e) {
cat.error("unable to recover subscriptions", e);
}
}
Aggregations