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

The following examples show how to use org.apache.activemq.artemis.api.jms.ActiveMQJMSClient#createQueue() . 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: OutgoingConnectionNoJTATest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleSendNoXAJMS1() throws Exception {
   Queue q = ActiveMQJMSClient.createQueue(MDBQUEUE);
   try (ClientSessionFactory sf = locator.createSessionFactory();
        ClientSession session = sf.createSession();
        ClientConsumer consVerify = session.createConsumer(MDBQUEUE);
        Connection conn = qraConnectionFactory.createConnection();
   ) {
      Session jmsSess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
      session.start();
      MessageProducer producer = jmsSess.createProducer(q);
      // These next 4 lines could be written in a single line however it makes difficult for debugging
      TextMessage msgsend = jmsSess.createTextMessage("hello");
      msgsend.setStringProperty("strvalue", "hello");
      producer.send(msgsend);

      ClientMessage msg = consVerify.receive(1000);
      assertNotNull(msg);
      assertEquals("hello", msg.getStringProperty("strvalue"));
   }
}
 
Example 2
Source File: AutoCreateJmsDestinationTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutoCreateOnConsumeFromQueue() throws Exception {
   Connection connection = null;
   connection = cf.createConnection();
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

   javax.jms.Queue queue = ActiveMQJMSClient.createQueue(QUEUE_NAME);

   MessageConsumer messageConsumer = session.createConsumer(queue);
   connection.start();

   Message m = messageConsumer.receive(500);
   Assert.assertNull(m);

   Queue q = (Queue) server.getPostOffice().getBinding(new SimpleString(QUEUE_NAME)).getBindable();
   Assert.assertEquals(0, q.getMessageCount());
   Assert.assertEquals(0, q.getMessagesAdded());
   connection.close();
}
 
Example 3
Source File: JmsConsumerTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testPreCommitAcksWithMessageExpirySetOnConnectionFactory() throws Exception {
   ((ActiveMQConnectionFactory) cf).setPreAcknowledge(true);
   conn = cf.createConnection();
   Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
   jBossQueue = ActiveMQJMSClient.createQueue(JmsConsumerTest.Q_NAME);
   MessageProducer producer = session.createProducer(jBossQueue);
   MessageConsumer consumer = session.createConsumer(jBossQueue);
   int noOfMessages = 1000;
   for (int i = 0; i < noOfMessages; i++) {
      TextMessage textMessage = session.createTextMessage("m" + i);
      producer.setTimeToLive(1);
      producer.send(textMessage);
   }

   Thread.sleep(2);

   conn.start();
   Message m = consumer.receiveNoWait();
   Assert.assertNull(m);

   // Asserting delivering count is zero is bogus since messages might still be being delivered and expired at this
   // point
   // which can cause delivering count to flip to 1
}
 
Example 4
Source File: JMSContextExample.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
public static void main(final String[] args) throws Exception {
   // Instantiate the queue
   Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");

   // Instantiate the ConnectionFactory (Using the default URI on this case)
   // Also instantiate the jmsContext
   // Using closeable interface
   try (ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
        JMSContext jmsContext = cf.createContext()) {
      // Create a message producer, note that we can chain all this into one statement
      jmsContext.createProducer().setDeliveryMode(DeliveryMode.PERSISTENT).send(queue, "this is a string");

      // Create a Consumer and receive the payload of the message direct.
      String payLoad = jmsContext.createConsumer(queue).receiveBody(String.class);

      System.out.println("payLoad = " + payLoad);

   }

}
 
Example 5
Source File: AutoCreateJmsDestinationTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutoCreateOnSendToQueueSecurity() throws Exception {
   ((ActiveMQJAASSecurityManager) server.getSecurityManager()).getConfiguration().addUser("guest", "guest");
   ((ActiveMQJAASSecurityManager) server.getSecurityManager()).getConfiguration().setDefaultUser("guest");
   ((ActiveMQJAASSecurityManager) server.getSecurityManager()).getConfiguration().addRole("guest", "rejectAll");
   Role role = new Role("rejectAll", false, false, false, false, false, false, false, false, false, false);
   Set<Role> roles = new HashSet<>();
   roles.add(role);
   server.getSecurityRepository().addMatch("#", roles);
   Connection connection = cf.createConnection();
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

   javax.jms.Queue queue = ActiveMQJMSClient.createQueue(QUEUE_NAME);

   try {
      session.createProducer(queue);
      Assert.fail("Sending a message here should throw a JMSSecurityException");
   } catch (Exception e) {
      Assert.assertTrue(e instanceof JMSSecurityException);
   }

   connection.close();
}
 
Example 6
Source File: MultipleProducersPagingTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
@Before
public void setUp() throws Exception {
   super.setUp();
   executor = Executors.newCachedThreadPool(ActiveMQThreadFactory.defaultThreadFactory());

   server = createServer(createBasicConfig()
                            .setPersistenceEnabled(false)
                            .setAddressesSettings(Collections.singletonMap("#", new AddressSettings()
                               .setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE)
                               .setPageSizeBytes(50000)
                               .setMaxSizeBytes(404850)))
                            .setAcceptorConfigurations(Collections.singleton(new TransportConfiguration(NettyAcceptorFactory.class.getName()))));

   server.start();

   cf = ActiveMQJMSClient.createConnectionFactory("tcp://127.0.0.1:61616", "cf");
   queue = ActiveMQJMSClient.createQueue("simple");

   barrierLatch = new CyclicBarrier(PRODUCERS + 1);
   runnersLatch = new CountDownLatch(PRODUCERS + 1);
   msgReceived = new AtomicLong(0);
   msgSent = new AtomicLong(0);
}
 
Example 7
Source File: OutgoingConnectionJTATest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleSendNoXAJMSContext() throws Exception {
   Queue q = ActiveMQJMSClient.createQueue(MDBQUEUE);

   try (ClientSessionFactory sf = locator.createSessionFactory();
        ClientSession session = sf.createSession();
        ClientConsumer consVerify = session.createConsumer(MDBQUEUE);
        JMSContext jmsctx = qraConnectionFactory.createContext();
   ) {
      session.start();
      // These next 4 lines could be written in a single line however it makes difficult for debugging
      JMSProducer producer = jmsctx.createProducer();
      producer.setProperty("strvalue", "hello");
      TextMessage msgsend = jmsctx.createTextMessage("hello");
      producer.send(q, msgsend);

      ClientMessage msg = consVerify.receive(1000);
      assertNotNull(msg);
      assertEquals("hello", msg.getStringProperty("strvalue"));
   }
}
 
Example 8
Source File: JmsConsumerTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Test
public void testCantReceiveWhenListenerIsSet() throws Exception {
   conn = cf.createConnection();
   Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
   jBossQueue = ActiveMQJMSClient.createQueue(JmsConsumerTest.Q_NAME);
   MessageConsumer consumer = session.createConsumer(jBossQueue);
   consumer.setMessageListener(new MessageListener() {
      @Override
      public void onMessage(final Message msg) {
      }
   });

   try {
      consumer.receiveNoWait();
      Assert.fail("Should throw exception");
   } catch (JMSException e) {
      // Ok
   }
}
 
Example 9
Source File: JmsConsumerTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testClearExceptionListener() throws Exception {
   conn = cf.createConnection();
   Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
   jBossQueue = ActiveMQJMSClient.createQueue(JmsConsumerTest.Q_NAME);
   MessageConsumer consumer = session.createConsumer(jBossQueue);
   consumer.setMessageListener(new MessageListener() {
      @Override
      public void onMessage(final Message msg) {
      }
   });

   consumer.setMessageListener(null);
   consumer.receiveNoWait();
}
 
Example 10
Source File: DestinationObjectFactoryTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testReference() throws Exception {
   ActiveMQDestination queue = (ActiveMQDestination) ActiveMQJMSClient.createQueue(RandomUtil.randomString());
   Reference reference = queue.getReference();
   String factoryName = reference.getFactoryClassName();
   Class<?> factoryClass = Class.forName(factoryName);
   ObjectFactory factory = (ObjectFactory) factoryClass.newInstance();
   Object object = factory.getObjectInstance(reference, null, null, null);
   Assert.assertNotNull(object);
   Assert.assertTrue(object instanceof ActiveMQDestination);
   Assert.assertEquals(queue, object);
}
 
Example 11
Source File: JmsConsumerTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testBrowserAndConsumerSimultaneousDifferentConnections() throws Exception {
   ((ActiveMQConnectionFactory) cf).setConsumerWindowSize(0);
   conn = cf.createConnection();

   Connection connConsumer = cf.createConnection();
   Session sessionConsumer = connConsumer.createSession(false, Session.AUTO_ACKNOWLEDGE);

   Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
   jBossQueue = ActiveMQJMSClient.createQueue(JmsConsumerTest.Q_NAME);
   MessageProducer producer = session.createProducer(jBossQueue);
   MessageConsumer consumer = sessionConsumer.createConsumer(jBossQueue);
   int noOfMessages = 1000;
   for (int i = 0; i < noOfMessages; i++) {
      TextMessage textMessage = session.createTextMessage("m" + i);
      textMessage.setIntProperty("i", i);
      producer.send(textMessage);
   }

   connConsumer.start();

   QueueBrowser browser = session.createBrowser(jBossQueue);
   Enumeration enumMessages = browser.getEnumeration();

   for (int i = 0; i < noOfMessages; i++) {
      TextMessage msg = (TextMessage) enumMessages.nextElement();
      Assert.assertNotNull(msg);
      Assert.assertEquals(i, msg.getIntProperty("i"));

      TextMessage recvMessage = (TextMessage) consumer.receiveNoWait();
      Assert.assertNotNull(recvMessage);
      Assert.assertEquals(i, msg.getIntProperty("i"));
   }

   Message m = consumer.receiveNoWait();
   Assert.assertFalse(enumMessages.hasMoreElements());
   Assert.assertNull(m);

   conn.close();
}
 
Example 12
Source File: ResourceAdapterTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnectionFactoryPropertiesApplyToRecoveryConfig() throws Exception {
   ServerLocator locator = createInVMNonHALocator();
   ClientSessionFactory factory = locator.createSessionFactory();
   ClientSession session = factory.createSession(false, false, false);
   ActiveMQDestination queue = (ActiveMQDestination) ActiveMQJMSClient.createQueue("test");
   session.createQueue(new QueueConfiguration(queue.getSimpleAddress()));
   session.close();

   ActiveMQResourceAdapter ra = new ActiveMQResourceAdapter();

   ra.setConnectorClassName(INVM_CONNECTOR_FACTORY);
   ra.setUserName("userGlobal");
   ra.setPassword("passwordGlobal");
   ra.setConnectionTTL(100L);
   ra.setCallFailoverTimeout(100L);
   ra.start(new BootstrapContext());

   Set<XARecoveryConfig> resources = ra.getRecoveryManager().getResources();
   assertEquals(100L, ra.getDefaultActiveMQConnectionFactory().getServerLocator().getConnectionTTL());
   assertEquals(100L, ra.getDefaultActiveMQConnectionFactory().getServerLocator().getCallFailoverTimeout());


   for (XARecoveryConfig resource : resources) {
      assertEquals(100L, resource.createServerLocator().getConnectionTTL());
      assertEquals(100L, resource.createServerLocator().getCallFailoverTimeout());
   }

   ra.stop();
   assertEquals(0, resources.size());
   locator.close();

}
 
Example 13
Source File: TestContextFactory.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
/**
 * Factory method to create new Queue instances
 */
protected Queue createQueue(String name) {
   return ActiveMQJMSClient.createQueue(name);
}
 
Example 14
Source File: ResourceAdapterTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testStartStopActivationManyTimes() throws Exception {
   ServerLocator locator = createInVMNonHALocator();
   ClientSessionFactory factory = locator.createSessionFactory();
   ClientSession session = factory.createSession(false, false, false);
   ActiveMQDestination queue = (ActiveMQDestination) ActiveMQJMSClient.createQueue("test");
   session.createQueue(new QueueConfiguration(queue.getSimpleAddress()));
   session.close();

   ActiveMQResourceAdapter ra = new ActiveMQResourceAdapter();

   ra.setConnectorClassName(INVM_CONNECTOR_FACTORY);
   ra.setUserName("userGlobal");
   ra.setPassword("passwordGlobal");
   ra.start(new BootstrapContext());

   Connection conn = ra.getDefaultActiveMQConnectionFactory().createConnection();

   conn.close();

   ActiveMQActivationSpec spec = new ActiveMQActivationSpec();

   spec.setResourceAdapter(ra);

   spec.setUseJNDI(false);

   spec.setUser("user");
   spec.setPassword("password");

   spec.setDestinationType("javax.jms.Topic");
   spec.setDestination("test");

   spec.setMinSession(1);
   spec.setMaxSession(15);

   ActiveMQActivation activation = new ActiveMQActivation(ra, new MessageEndpointFactory(), spec);

   ServerLocatorImpl serverLocator = (ServerLocatorImpl) ra.getDefaultActiveMQConnectionFactory().getServerLocator();

   Set<XARecoveryConfig> resources = ra.getRecoveryManager().getResources();

   for (int i = 0; i < 10; i++) {
      activation.start();
      assertEquals(1, resources.size());
      activation.stop();
   }

   ra.stop();
   assertEquals(0, resources.size());
   locator.close();

}
 
Example 15
Source File: JmsConsumerTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
@Test
public void testBrowserAndConsumerSimultaneous() throws Exception {
   ((ActiveMQConnectionFactory) cf).setConsumerWindowSize(0);
   conn = cf.createConnection();

   Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
   jBossQueue = ActiveMQJMSClient.createQueue(JmsConsumerTest.Q_NAME);
   MessageProducer producer = session.createProducer(jBossQueue);

   QueueBrowser browser = session.createBrowser(jBossQueue);
   Enumeration enumMessages = browser.getEnumeration();

   MessageConsumer consumer = session.createConsumer(jBossQueue);
   int noOfMessages = 10;
   for (int i = 0; i < noOfMessages; i++) {
      TextMessage textMessage = session.createTextMessage("m" + i);
      textMessage.setIntProperty("i", i);
      producer.send(textMessage);
   }

   conn.start();
   for (int i = 0; i < noOfMessages; i++) {
      TextMessage msg = (TextMessage) enumMessages.nextElement();
      Assert.assertNotNull(msg);
      Assert.assertEquals(i, msg.getIntProperty("i"));

      conn.start();
      TextMessage recvMessage = (TextMessage) consumer.receiveNoWait();
      Assert.assertNotNull(recvMessage);
      conn.stop();
      Assert.assertEquals(i, msg.getIntProperty("i"));
   }

   Assert.assertNull(consumer.receiveNoWait());
   Assert.assertFalse(enumMessages.hasMoreElements());

   conn.close();

   // Asserting delivering count is zero is bogus since messages might still be being delivered and expired at this
   // point
   // which can cause delivering count to flip to 1
}
 
Example 16
Source File: ArtemisJmsConfiguration.java    From artemis-disruptor-miaosha with Apache License 2.0 4 votes vote down vote up
@Bean
public Queue defaultQueue() {
  return ActiveMQJMSClient.createQueue(artemisProperties.getQueue());
}
 
Example 17
Source File: PreacknowledgeExample.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
private static int getMessageCount(final Connection connection) throws Exception {
   QueueSession session = ((QueueConnection) connection).createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

   Queue managementQueue = ActiveMQJMSClient.createQueue("activemq.management");

   QueueRequestor requestor = new QueueRequestor(session, managementQueue);

   connection.start();

   Message m = session.createMessage();

   JMSManagementHelper.putAttribute(m, ResourceNames.QUEUE + "exampleQueue", "messageCount");

   Message response = requestor.request(m);

   int messageCount = (Integer) JMSManagementHelper.getResult(response, Integer.class);

   return messageCount;
}
 
Example 18
Source File: ActiveMQInitialContextFactory.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
/**
 * Factory method to create new Queue instances
 */
protected Queue createQueue(String name) {
   return ActiveMQJMSClient.createQueue(name);
}
 
Example 19
Source File: MessagePriorityExample.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception {
   AtomicBoolean result = new AtomicBoolean(true);
   final ArrayList<TextMessage> msgReceived = new ArrayList<>();
   Connection connection = null;
   try {

      // Step 2. look-up the JMS queue object from JNDI
      Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");

      // Step 3. look-up the JMS connection factory object from JNDI
      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. Create a JMS Message Producer
      MessageProducer producer = session.createProducer(queue);
      // Step 8. Create three messages
      TextMessage[] sentMessages = new TextMessage[3];
      sentMessages[0] = session.createTextMessage("first message");
      sentMessages[1] = session.createTextMessage("second message");
      sentMessages[2] = session.createTextMessage("third message");

      // Step 9. Send the Messages, each has a different priority
      producer.send(sentMessages[0]);
      System.out.println("Message sent: " + sentMessages[0].getText() +
                            " with priority: " +
                            sentMessages[0].getJMSPriority());
      producer.send(sentMessages[1], DeliveryMode.NON_PERSISTENT, 5, 0);
      System.out.println("Message sent: " + sentMessages[1].getText() +
                            "with priority: " +
                            sentMessages[1].getJMSPriority());
      producer.send(sentMessages[2], DeliveryMode.NON_PERSISTENT, 9, 0);
      System.out.println("Message sent: " + sentMessages[2].getText() +
                            "with priority: " +
                            sentMessages[2].getJMSPriority());

      MessageConsumer redConsumer = session.createConsumer(queue);
      redConsumer.setMessageListener(new SimpleMessageListener(msgReceived, result));

      // Step 10. Start the connection now.
      connection.start();

      // Step 11. Wait for message delivery completion
      Thread.sleep(5000);

      // Step 12. Examine the order
      for (int i = 0; i < 3; i++) {
         TextMessage rm = msgReceived.get(i);
         if (!rm.getText().equals(sentMessages[2 - i].getText())) {
            throw new IllegalStateException("Priority is broken!");
         }
      }

      if (!result.get())
         throw new IllegalStateException();
   } finally {
      // Step 13. Be sure to close our JMS resources!
      if (connection != null) {
         connection.close();
      }
   }
}
 
Example 20
Source File: ReattachExample.java    From activemq-artemis with Apache License 2.0 3 votes vote down vote up
private static void stopStartAcceptor(final boolean stop) throws Exception {
   ConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:61617");

   Connection connection = null;
   try {
      connection = cf.createConnection();

      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

      Queue managementQueue = ActiveMQJMSClient.createQueue("activemq.management");

      MessageProducer producer = session.createProducer(managementQueue);

      connection.start();

      Message m = session.createMessage();

      String oper = stop ? "stop" : "start";

      JMSManagementHelper.putOperationInvocation(m, "core.acceptor.netty-acceptor", oper);

      producer.send(m);
   } finally {
      if (connection != null) {
         connection.close();
      }
   }
}