Java Code Examples for org.apache.activemq.artemis.api.jms.ActiveMQJMSClient#createTopic()

The following examples show how to use org.apache.activemq.artemis.api.jms.ActiveMQJMSClient#createTopic() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BrokerProducerThread.java    From OpenIoE with Apache License 2.0 6 votes vote down vote up
public void StartBrokerProducer() throws JMSException {

        Connection connection = null;
        try {
            ConnectionFactory cf = new ActiveMQConnectionFactory(ioeConfiguration.getTopic().getTopicUrl());
            connection = cf.createConnection(ioeConfiguration.getTopic().getUsername(), ioeConfiguration.getTopic().getPassword());
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // TODO - Read message payload from cassandra
            String payload = "{\"data\": \"36\",\"description\": \"test\",\"sensorId\": 1, \"timestamp\": \"2015-06-19T11:07:44.526Z\", \"topic\": \"topic\"}";
            Message msg = session.createTextMessage(payload);

            //TODO Read topic value from the sensor data or publication entity
            Topic topic = ActiveMQJMSClient.createTopic("ioe");
            MessageProducer messageProducer = session.createProducer(null);
            messageProducer.send(topic,msg);
            connection.start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
 
Example 2
Source File: JmsConsumerTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testSharedDurableConsumer() throws Exception {
   conn = cf.createConnection();
   conn.start();
   Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
   topic = ActiveMQJMSClient.createTopic(T_NAME);

   MessageConsumer cons = session.createSharedDurableConsumer(topic, "test1");

   MessageProducer producer = session.createProducer(topic);

   producer.send(session.createTextMessage("test"));

   TextMessage txt = (TextMessage) cons.receive(5000);

   Assert.assertNotNull(txt);
}
 
Example 3
Source File: JmsConsumerTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testSharedConsumer() throws Exception {
   conn = cf.createConnection();
   conn.start();
   Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
   topic = ActiveMQJMSClient.createTopic(T_NAME);

   MessageConsumer cons = session.createSharedConsumer(topic, "test1");

   MessageProducer producer = session.createProducer(topic);

   producer.send(session.createTextMessage("test"));

   TextMessage txt = (TextMessage) cons.receive(5000);

   Assert.assertNotNull(txt);
}
 
Example 4
Source File: AutoCreateJmsDestinationTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutoCreateOnDurableSubscribeToTopic() throws Exception {
   Connection connection = cf.createConnection();
   connection.setClientID("myClientID");
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

   javax.jms.Topic topic = ActiveMQJMSClient.createTopic(QUEUE_NAME);

   MessageConsumer consumer = session.createDurableConsumer(topic, "myDurableSub");
   MessageProducer producer = session.createProducer(topic);
   producer.send(session.createTextMessage("msg"));
   connection.start();
   assertNotNull(consumer.receive(500));

   connection.close();

   assertNotNull(server.getManagementService().getResource(ResourceNames.ADDRESS + "test"));

   assertNotNull(server.locateQueue(SimpleString.toSimpleString("myClientID.myDurableSub")));
}
 
Example 5
Source File: AutoCreateJmsDestinationTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutoCreateOnSubscribeToTopic() throws Exception {
   Connection connection = cf.createConnection();
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   final String topicName = "test-" + UUID.randomUUID().toString();

   javax.jms.Topic topic = ActiveMQJMSClient.createTopic(topicName);

   MessageConsumer consumer = session.createConsumer(topic);
   MessageProducer producer = session.createProducer(topic);
   producer.send(session.createTextMessage("msg"));
   connection.start();
   assertNotNull(consumer.receive(500));

   assertNotNull(server.getManagementService().getResource(ResourceNames.ADDRESS + topicName));

   connection.close();

   assertNull(server.getManagementService().getResource(ResourceNames.ADDRESS + topicName));
}
 
Example 6
Source File: BrokerConsumerThread.java    From OpenIoE with Apache License 2.0 6 votes vote down vote up
public void StartBrokerConsumer() throws JMSException {
//        ApplicationContext context = new AnnotationConfigApplicationContext(IoeConfiguration.class);
//        IoeConfiguration ioeConfiguration = context.getBean(IoeConfiguration.class);

        Connection connection = null;
        try {
            ConnectionFactory cf = new ActiveMQConnectionFactory(ioeConfiguration.getTopic().getTopicUrl());
            connection = cf.createConnection(ioeConfiguration.getTopic().getUsername(), ioeConfiguration.getTopic().getPassword());
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            List<Subscription> subscriptions = subscriptionRepository.findAll();
            for(Subscription s:subscriptions) {
                Topic topic = ActiveMQJMSClient.createTopic(s.getTopicFilter());
                MessageConsumer messageConsumer = session.createConsumer(topic);
                messageConsumer.setMessageListener(new BrokerMessageListener(sensorRepository, databaseService));
            }
            connection.start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
 
Example 7
Source File: AutoCreateJmsDestinationTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoCreateOnSendToTopic() throws Exception {
   Connection connection = cf.createConnection();
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

   javax.jms.Topic topic = ActiveMQJMSClient.createTopic(QUEUE_NAME);

   MessageProducer producer = session.createProducer(topic);
   producer.send(session.createTextMessage("msg"));

   connection.close();

   assertNotNull(server.getManagementService().getResource(ResourceNames.ADDRESS + "test"));
}
 
Example 8
Source File: AutoDeleteJmsDestinationTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoDeleteTopic() throws Exception {
   Connection connection = cf.createConnection();
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

   javax.jms.Topic topic = ActiveMQJMSClient.createTopic("test");

   MessageConsumer messageConsumer = session.createConsumer(topic);
   MessageProducer producer = session.createProducer(topic);

   final int numMessages = 100;

   for (int i = 0; i < numMessages; i++) {
      TextMessage mess = session.createTextMessage("msg" + i);
      producer.send(mess);
   }

   producer.close();
   connection.start();

   for (int i = 0; i < numMessages; i++) {
      Message m = messageConsumer.receive(5000);
      Assert.assertNotNull(m);
   }

   connection.close();

   SimpleString qName = new SimpleString("test");
   Wait.waitFor(() -> server.locateQueue(qName) == null);
   // ensure the topic was removed
   Assert.assertNull(server.locateQueue(qName));

   // make sure the JMX control was removed for the JMS topic
   assertNull(server.getManagementService().getResource("jtest"));
}
 
Example 9
Source File: AutoDeleteJmsDestinationTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoDeleteTopicDurableSubscriber() throws Exception {
   Connection connection = cf.createConnection();
   connection.setClientID("myClientID");
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

   javax.jms.Topic topic = ActiveMQJMSClient.createTopic("test");

   MessageConsumer messageConsumer = session.createDurableConsumer(topic, "mySub");
   MessageProducer producer = session.createProducer(topic);

   final int numMessages = 100;

   for (int i = 0; i < numMessages; i++) {
      TextMessage mess = session.createTextMessage("msg" + i);
      producer.send(mess);
   }

   producer.close();
   connection.start();

   for (int i = 0; i < numMessages; i++) {
      Message m = messageConsumer.receive(5000);
      Assert.assertNotNull(m);
   }

   messageConsumer.close();
   session.unsubscribe("mySub");

   connection.close();

   // ensure the topic was removed
   Assert.assertNull(server.locateQueue(new SimpleString("test")));

   // make sure the JMX control was removed for the JMS topic
   assertNull(server.getManagementService().getResource("test"));
}
 
Example 10
Source File: JmsConsumerTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
@Before
public void setUp() throws Exception {
   super.setUp();

   topic = ActiveMQJMSClient.createTopic(T_NAME);
   topic2 = ActiveMQJMSClient.createTopic(T2_NAME);

   jmsServer.createQueue(false, JmsConsumerTest.Q_NAME, null, true, JmsConsumerTest.Q_NAME);
   jmsServer.createTopic(true, T_NAME, "/topic/" + T_NAME);
   jmsServer.createTopic(true, T2_NAME, "/topic/" + T2_NAME);
   cf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY));
}
 
Example 11
Source File: JmsConsumerTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testSharedDurableConsumerWithClientID() throws Exception {
   conn = cf.createConnection();
   conn.setClientID("C1");
   conn.start();
   Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

   Connection conn2 = cf.createConnection();
   conn2.setClientID("C2");
   Session session2 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);

   {
      Connection conn3 = cf.createConnection();

      boolean exception = false;
      try {
         conn3.setClientID("C2");
      } catch (Exception e) {
         exception = true;
      }

      Assert.assertTrue(exception);
      conn3.close();
   }

   topic = ActiveMQJMSClient.createTopic(T_NAME);

   MessageConsumer cons = session.createSharedDurableConsumer(topic, "test1");

   MessageProducer producer = session.createProducer(topic);

   producer.send(session.createTextMessage("test"));

   TextMessage txt = (TextMessage) cons.receive(5000);

   Assert.assertNotNull(txt);
}
 
Example 12
Source File: TopicHierarchyExample.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception {
   Connection connection = null;
   try {
      ConnectionFactory cf = new ActiveMQConnectionFactory();

      // Step 4. Create a JMS Connection
      connection = cf.createConnection();

      // Step 5. Create a JMS Session
      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

      // Step 6. Instantiate a topic representing the wildcard we're going to subscribe to
      Topic topicSubscribe = ActiveMQJMSClient.createTopic("news.europe.#");

      // Step 7. Create a consumer (topic subscriber) that will consume using that wildcard
      // The consumer will receive any messages sent to any topic that starts with news.europe
      MessageConsumer messageConsumer = session.createConsumer(topicSubscribe);

      // Step 8. Create an anonymous producer
      MessageProducer producer = session.createProducer(null);

      // Step 9. Instantiate some more topic objects corresponding to the individual topics
      // we're going to send messages to
      Topic topicNewsUsaWrestling = ActiveMQJMSClient.createTopic("news.usa.wrestling");

      Topic topicNewsEuropeSport = ActiveMQJMSClient.createTopic("news.europe.sport");

      Topic topicNewsEuropeEntertainment = ActiveMQJMSClient.createTopic("news.europe.entertainment");

      // Step 10. Send a message destined for the usa wrestling topic
      TextMessage messageWrestlingNews = session.createTextMessage("Hulk Hogan starts ballet classes");

      producer.send(topicNewsUsaWrestling, messageWrestlingNews);

      // Step 11. Send a message destined for the europe sport topic
      TextMessage messageEuropeSport = session.createTextMessage("Lewis Hamilton joins European synchronized swimming team");

      producer.send(topicNewsEuropeSport, messageEuropeSport);

      // Step 12. Send a message destined for the europe entertainment topic
      TextMessage messageEuropeEntertainment = session.createTextMessage("John Lennon resurrected from dead");

      producer.send(topicNewsEuropeEntertainment, messageEuropeEntertainment);

      // Step 9. Start the connection

      connection.start();

      // Step 10. We don't receive the usa wrestling message since we subscribed to news.europe.# and
      // that doesn't match news.usa.wrestling. However we do receive the Europe sport message, and the
      // europe entertainment message, since these match the wildcard.

      TextMessage messageReceived1 = (TextMessage) messageConsumer.receive(5000);

      System.out.println("Received message: " + messageReceived1.getText());

      TextMessage messageReceived2 = (TextMessage) messageConsumer.receive(5000);

      System.out.println("Received message: " + messageReceived2.getText());

      Message message = messageConsumer.receive(1000);

      if (message != null) {
         throw new IllegalStateException("Message was not null.");
      }

      System.out.println("Didn't received any more message: " + message);
   } finally {
      // Step 12. Be sure to close our resources!
      if (connection != null) {
         connection.close();
      }
   }
}
 
Example 13
Source File: ArtemisJmsConfiguration.java    From artemis-disruptor-miaosha with Apache License 2.0 4 votes vote down vote up
@Bean
public Topic defaultTopic() {
  return ActiveMQJMSClient.createTopic(artemisProperties.getTopic());
}
 
Example 14
Source File: TestContextFactory.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
/**
 * Factory method to create new Topic instances
 */
protected Topic createTopic(String name) {
   return ActiveMQJMSClient.createTopic(name);
}
 
Example 15
Source File: ActiveMQInitialContextFactory.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
/**
 * Factory method to create new Topic instances
 */
protected Topic createTopic(String name) {
   return ActiveMQJMSClient.createTopic(name);
}
 
Example 16
Source File: JMSReconnectTest.java    From activemq-artemis with Apache License 2.0 3 votes vote down vote up
@Test
public void testNoReconnectCloseAfterFailToReconnectWithTopicConsumer() throws Exception {
   ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY));

   jbcf.setReconnectAttempts(0);

   Connection conn = jbcf.createConnection();

   Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

   ClientSession coreSession = ((ActiveMQSession) sess).getCoreSession();

   coreSession.createQueue(new QueueConfiguration("blahblah").setAddress("mytopic").setDurable(false));

   Topic topic = ActiveMQJMSClient.createTopic("mytopic");

   //Create a non durable subscriber
   sess.createConsumer(topic);

   Thread.sleep(2000);

   this.server.stop();

   this.server.start();

   sess.close();

   conn.close();
}
 
Example 17
Source File: ClusteredTopicExample.java    From activemq-artemis with Apache License 2.0 2 votes vote down vote up
public static void main(final String[] args) throws Exception {
   Connection connection0 = null;

   Connection connection1 = null;

   try {

      // Step 1. Instantiate topic
      Topic topic = ActiveMQJMSClient.createTopic("exampleTopic");

      // Step 2. Look-up a JMS Connection Factory object from JNDI on server 0
      ConnectionFactory cf0 = new ActiveMQConnectionFactory("tcp://localhost:61616");

      // Step 3. Look-up a JMS Connection Factory object from JNDI on server 1
      ConnectionFactory cf1 = new ActiveMQConnectionFactory("tcp://localhost:61617");

      // Step 4. We create a JMS Connection connection0 which is a connection to server 0
      connection0 = cf0.createConnection();

      // Step 5. We create a JMS Connection connection1 which is a connection to server 1
      connection1 = cf1.createConnection();

      // Step 6. We create a JMS Session on server 0
      Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);

      // Step 7. We create a JMS Session on server 1
      Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);

      // Step 8. We start the connections to ensure delivery occurs on them
      connection0.start();

      connection1.start();

      // Step 9. We create JMS MessageConsumer objects on server 0 and server 1
      MessageConsumer consumer0 = session0.createConsumer(topic);

      MessageConsumer consumer1 = session1.createConsumer(topic);

      Thread.sleep(1000);

      // Step 10. We create a JMS MessageProducer object on server 0
      MessageProducer producer = session0.createProducer(topic);

      // Step 11. We send some messages to server 0

      final int numMessages = 10;

      for (int i = 0; i < numMessages; i++) {
         TextMessage message = session0.createTextMessage("This is text message " + i);

         producer.send(message);

         System.out.println("Sent message: " + message.getText());
      }

      // Step 12. We now consume those messages on *both* server 0 and server 1.
      // We note that all messages have been consumed by *both* consumers.
      // JMS Topics implement *publish-subscribe* messaging where all consumers get a copy of all messages

      for (int i = 0; i < numMessages; i++) {
         TextMessage message0 = (TextMessage) consumer0.receive(5000);

         System.out.println("Got message: " + message0.getText() + " from node 0");

         TextMessage message1 = (TextMessage) consumer1.receive(5000);

         System.out.println("Got message: " + message1.getText() + " from node 1");
      }
   } finally {
      // Step 15. Be sure to close our JMS resources!
      if (connection0 != null) {
         connection0.close();
      }

      if (connection1 != null) {
         connection1.close();
      }
   }
}
 
Example 18
Source File: ClusteredTopicExample.java    From activemq-artemis with Apache License 2.0 2 votes vote down vote up
public static void main(final String[] args) throws Exception {
   Connection connection0 = null;

   Connection connection1 = null;

   try {

      // Step 1. Instantiate topic
      Topic topic = ActiveMQJMSClient.createTopic("exampleTopic");

      // Step 2. Look-up a JMS Connection Factory object from JNDI on server 0
      ConnectionFactory cf0 = new ActiveMQConnectionFactory("tcp://localhost:61616");

      // Step 3. Look-up a JMS Connection Factory object from JNDI on server 1
      ConnectionFactory cf1 = new ActiveMQConnectionFactory("tcp://localhost:61617");

      // Step 4. We create a JMS Connection connection0 which is a connection to server 0
      connection0 = cf0.createConnection();

      // Step 5. We create a JMS Connection connection1 which is a connection to server 1
      connection1 = cf1.createConnection();

      // Step 6. We create a JMS Session on server 0
      Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);

      // Step 7. We create a JMS Session on server 1
      Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);

      // Step 8. We start the connections to ensure delivery occurs on them
      connection0.start();

      connection1.start();

      // Step 9. We create JMS MessageConsumer objects on server 0 and server 1
      MessageConsumer consumer0 = session0.createConsumer(topic);

      MessageConsumer consumer1 = session1.createConsumer(topic);

      Thread.sleep(1000);

      // Step 10. We create a JMS MessageProducer object on server 0
      MessageProducer producer = session0.createProducer(topic);

      // Step 11. We send some messages to server 0

      final int numMessages = 10;

      for (int i = 0; i < numMessages; i++) {
         TextMessage message = session0.createTextMessage("This is text message " + i);

         producer.send(message);

         System.out.println("Sent message: " + message.getText());
      }

      // Step 12. We now consume those messages on *both* server 0 and server 1.
      // We note that all messages have been consumed by *both* consumers.
      // JMS Topics implement *publish-subscribe* messaging where all consumers get a copy of all messages

      for (int i = 0; i < numMessages; i++) {
         TextMessage message0 = (TextMessage) consumer0.receive(5000);

         System.out.println("Got message: " + message0.getText() + " from node 0");

         TextMessage message1 = (TextMessage) consumer1.receive(5000);

         System.out.println("Got message: " + message1.getText() + " from node 1");
      }
   } finally {
      // Step 15. Be sure to close our JMS resources!
      if (connection0 != null) {
         connection0.close();
      }

      if (connection1 != null) {
         connection1.close();
      }
   }
}
 
Example 19
Source File: FederatedAddressDivertExample.java    From activemq-artemis with Apache License 2.0 2 votes vote down vote up
public static void main(final String[] args) throws Exception {
   Connection connectionEUWest = null;

   Connection connectionEUEast = null;


   try {
      // Step 1. Instantiate the Topic (multicast) for the producers
      Topic topic = ActiveMQJMSClient.createTopic("exampleTopic");

      //Create a topic for the consumers
      Topic topic2 = ActiveMQJMSClient.createTopic("divertExampleTopic");

      // Step 2. Instantiate connection towards server EU West
      ConnectionFactory cfEUWest = new ActiveMQConnectionFactory("tcp://localhost:61616");

      // Step 3. Instantiate connection towards server EU East
      ConnectionFactory cfEUEast = new ActiveMQConnectionFactory("tcp://localhost:61617");


      // Step 5. We create a JMS Connection connectionEUWest which is a connection to server EU West
      connectionEUWest = cfEUWest.createConnection();

      // Step 6. We create a JMS Connection connectionEUEast which is a connection to server EU East
      connectionEUEast = cfEUEast.createConnection();

      // Step 8. We create a JMS Session on server EU West
      Session sessionEUWest = connectionEUWest.createSession(false, Session.AUTO_ACKNOWLEDGE);

      // Step 9. We create a JMS Session on server EU East
      Session sessionEUEast = connectionEUEast.createSession(false, Session.AUTO_ACKNOWLEDGE);

      // Step 11. We start the connections to ensure delivery occurs on them
      connectionEUWest.start();

      connectionEUEast.start();

      // Step 12. We create a JMS MessageProducer object on each server
      MessageProducer producerEUEast = sessionEUEast.createProducer(topic);

      // Step 13. We create JMS MessageConsumer objects on each server - Messages will be diverted to this topic
      MessageConsumer consumerEUWest = sessionEUWest.createSharedDurableConsumer(topic2, "exampleSubscription");


      // Step 14. Let a little time for everything to start and form.

      Thread.sleep(5000);

      // Step 13. We send some messages to server EU West
      final int numMessages = 10;

      // Step 15. Repeat same test one last time, this time sending on EU East

      for (int i = 0; i < numMessages; i++) {
         TextMessage message = sessionEUEast.createTextMessage("This is text sent from EU East, message " + i);

         producerEUEast.send(message);

         System.out.println("EU East   :: Sent message: " + message.getText());
      }

      // Step 14. We now consume those messages on *all* servers .
      // We note that every consumer, receives a message even so on seperate servers

      for (int i = 0; i < numMessages; i++) {
         TextMessage messageEUWest = (TextMessage) consumerEUWest.receive(5000);

         System.out.println("EU West   :: Got message: " + messageEUWest.getText());
      }
   } finally {
      // Step 16. Be sure to close our resources!
      if (connectionEUWest != null) {
         connectionEUWest.stop();
         connectionEUWest.close();
      }

      if (connectionEUEast != null) {
         connectionEUEast.stop();
         connectionEUEast.close();
      }
   }
}