Search in sources :

Example 1 with TopicPublisher

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();
}
Also used : TopicSubscriber(javax.jms.TopicSubscriber) TopicSession(javax.jms.TopicSession) TopicPublisher(javax.jms.TopicPublisher) ACSJMSTextMessage(com.cosylab.acs.jms.ACSJMSTextMessage) Topic(javax.jms.Topic) TopicConnection(javax.jms.TopicConnection)

Example 2 with TopicPublisher

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);
            }
        }
    }
}
Also used : TopicSession(javax.jms.TopicSession) BrokerException(org.wso2.carbon.apimgt.core.exception.BrokerException) TopicPublisher(javax.jms.TopicPublisher) GatewayException(org.wso2.carbon.apimgt.core.exception.GatewayException) Gson(com.google.gson.Gson) JMSException(javax.jms.JMSException) Topic(javax.jms.Topic) TopicConnection(javax.jms.TopicConnection) TextMessage(javax.jms.TextMessage)

Example 3 with TopicPublisher

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);
}
Also used : TopicSubscriber(javax.jms.TopicSubscriber) TopicSession(javax.jms.TopicSession) TopicPublisher(javax.jms.TopicPublisher) Topic(javax.jms.Topic) TextMessage(javax.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Example 4 with TopicPublisher

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());
}
Also used : TopicSubscriber(javax.jms.TopicSubscriber) TopicSession(javax.jms.TopicSession) TopicPublisher(javax.jms.TopicPublisher) Topic(javax.jms.Topic) TextMessage(javax.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Example 5 with TopicPublisher

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");
}
Also used : TopicSubscriber(javax.jms.TopicSubscriber) TopicSession(javax.jms.TopicSession) TopicPublisher(javax.jms.TopicPublisher) Topic(javax.jms.Topic) TextMessage(javax.jms.TextMessage) Test(org.junit.jupiter.api.Test)

Aggregations

TopicPublisher (javax.jms.TopicPublisher)36 TopicSession (javax.jms.TopicSession)27 Topic (javax.jms.Topic)23 TextMessage (javax.jms.TextMessage)22 TopicSubscriber (javax.jms.TopicSubscriber)22 Test (org.junit.Test)15 TopicConnection (javax.jms.TopicConnection)14 Message (javax.jms.Message)11 Test (org.junit.jupiter.api.Test)8 JMSException (javax.jms.JMSException)4 Span (brave.Span)3 SpanInScope (brave.Tracer.SpanInScope)3 Connection (javax.jms.Connection)3 RMQTextMessage (com.rabbitmq.jms.client.message.RMQTextMessage)2 BytesMessage (javax.jms.BytesMessage)2 MapMessage (javax.jms.MapMessage)2 ObjectMessage (javax.jms.ObjectMessage)2 NamingException (javax.naming.NamingException)2 Type (org.candlepin.audit.Event.Type)2 ACSJMSTextMessage (com.cosylab.acs.jms.ACSJMSTextMessage)1