use of javax.jms.TopicPublisher 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.TopicPublisher in project carbon-apimgt by wso2.
the class BrokerUtil method publishToTopic.
/**
* Publish to broker topic
*
* @param topicName publishing topic name
* @param gatewayEvent topic message data object
*/
public static void publishToTopic(String topicName, GatewayEvent gatewayEvent) throws GatewayException {
TopicSession topicSession = null;
Topic topic = null;
TopicPublisher topicPublisher = null;
TopicConnection topicConnection = null;
try {
topicConnection = getTopicConnection();
topicConnection.start();
topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
topic = topicSession.createTopic(topicName);
topicPublisher = topicSession.createPublisher(topic);
TextMessage textMessage = topicSession.createTextMessage(new Gson().toJson(gatewayEvent));
topicPublisher.publish(textMessage);
} catch (JMSException e) {
String errorMessage = "Error occurred while publishing " + gatewayEvent.getEventType() + " event to JMS " + "topic :" + topicName;
log.error(errorMessage, e);
throw new GatewayException(errorMessage, ExceptionCodes.GATEWAY_EXCEPTION);
} catch (BrokerException e) {
String errorMessage = "Error occurred while obtaining broker topic connection for topic : " + topicName;
log.error(errorMessage, e);
throw new GatewayException(errorMessage, ExceptionCodes.GATEWAY_EXCEPTION);
} finally {
if (topicPublisher != null) {
try {
topicPublisher.close();
} catch (JMSException e) {
log.error("Error occurred while closing topic publisher for topic : " + topicName);
}
}
if (topicSession != null) {
try {
topicSession.close();
} catch (JMSException e) {
log.error("Error occurred while closing topic session for topic : " + topicName);
}
}
if (topicConnection != null) {
try {
topicConnection.close();
} catch (JMSException e) {
log.error("Error occurred while closing topic connection for topic : " + topicName);
}
}
}
}
use of javax.jms.TopicPublisher in project rabbitmq-jms-client by rabbitmq.
the class SimpleDurableTopicMessageIT method testSendAndReceiveTextMessage.
@Test
public void testSendAndReceiveTextMessage() throws Exception {
topicConn.start();
TopicSession topicSession = topicConn.createTopicSession(false, Session.DUPS_OK_ACKNOWLEDGE);
Topic topic = topicSession.createTopic(TOPIC_NAME);
TopicPublisher sender = topicSession.createPublisher(topic);
TopicSubscriber receiver1 = topicSession.createDurableSubscriber(topic, DURABLE_SUBSCRIBER_NAME);
TopicSubscriber receiver2 = topicSession.createSubscriber(topic);
sender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage message = topicSession.createTextMessage(MESSAGE_TEXT_1);
sender.send(message);
assertEquals(MESSAGE_TEXT_1, ((TextMessage) receiver1.receive()).getText());
assertEquals(MESSAGE_TEXT_1, ((TextMessage) receiver2.receive()).getText());
topicSession.unsubscribe(DURABLE_SUBSCRIBER_NAME);
}
use of javax.jms.TopicPublisher in project rabbitmq-jms-client by rabbitmq.
the class SimpleTopicMessageIT method testSendAndReceiveTextMessage.
@Test
public void testSendAndReceiveTextMessage() throws Exception {
topicConn.start();
TopicSession topicSession = topicConn.createTopicSession(false, Session.DUPS_OK_ACKNOWLEDGE);
Topic topic = topicSession.createTopic(TOPIC_NAME);
TopicPublisher sender = topicSession.createPublisher(topic);
TopicSubscriber receiver1 = topicSession.createSubscriber(topic);
TopicSubscriber receiver2 = topicSession.createSubscriber(topic);
sender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage message = topicSession.createTextMessage(MESSAGE_TEXT_1);
sender.send(message);
assertEquals(MESSAGE_TEXT_1, ((TextMessage) receiver1.receive()).getText());
assertEquals(MESSAGE_TEXT_1, ((TextMessage) receiver2.receive()).getText());
}
use of javax.jms.TopicPublisher in project rabbitmq-jms-client by rabbitmq.
the class SimpleTopicMessageIT method testSendAndReceiveWildTopicTestMessage.
@Test
public void testSendAndReceiveWildTopicTestMessage() throws Exception {
topicConn.start();
TopicSession topicSession = topicConn.createTopicSession(false, Session.DUPS_OK_ACKNOWLEDGE);
Topic explicitTopicDestination = topicSession.createTopic(TOPIC_NAME);
Topic wildTopicDestination = topicSession.createTopic(WILD_TOPIC_NAME);
TopicPublisher sender = topicSession.createPublisher(explicitTopicDestination);
TopicSubscriber receiver = topicSession.createSubscriber(wildTopicDestination);
sender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
TextMessage message = topicSession.createTextMessage(MESSAGE_TEXT_2);
sender.send(message);
TextMessage textMessage = (TextMessage) receiver.receive();
assertEquals(MESSAGE_TEXT_2, textMessage.getText(), "Failed to receive proper text message");
assertEquals(explicitTopicDestination, textMessage.getJMSDestination(), "Incorrect publisher destination on received message");
}
Aggregations