Java Code Examples for javax.jms.JMSContext#createQueue()

The following examples show how to use javax.jms.JMSContext#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: JmsEventChannelTestCase.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@After
public void stop() throws Exception {
    JMSContext context = factory.createContext();
    Destination channel = context.createQueue(MockEvent.class.getName());
    JMSConsumer consumer = context.createConsumer(channel);
    // 清理测试消息
    logger.info("清理JMS测试消息开始");
    AtomicInteger count = new AtomicInteger();
    consumer.setMessageListener((data) -> {
        String message = StringUtility.format("清理JMS测试消息[{}]", count.incrementAndGet());
        logger.info(message);
    });
    Thread.sleep(1000L);
    logger.info("清理JMS测试消息结束");
    factory.close();
}
 
Example 2
Source File: JMSConsumerIntegrationTest.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 20000)
public void testCreateConsumer() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        JMSContext context = testFixture.createJMSContext(testPeer);
        testPeer.expectBegin();
        testPeer.expectReceiverAttach();
        testPeer.expectLinkFlow();

        Queue queue = context.createQueue("test");
        JMSConsumer consumer = context.createConsumer(queue);
        assertNotNull(consumer);

        testPeer.expectEnd();
        testPeer.expectClose();
        context.close();

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
Example 3
Source File: JmsSink.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
private Destination getDestination(JMSContext context, String name, String type) {
    switch (type.toLowerCase()) {
        case "queue":
            return context.createQueue(name);
        case "topic":
            return context.createTopic(name);
        default:
            throw ex.illegalStateUnknownDestinationType(type);
    }

}
 
Example 4
Source File: JMSConsumerIntegrationTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
public void doTestReceiveBodyFailsDoesNotAcceptMessage(int sessionMode) throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        JMSContext context = testFixture.createJMSContext(testPeer);

        testPeer.expectBegin();

        final String content = "Message-Content";
        Queue queue = context.createQueue("myQueue");

        DescribedType amqpValueContent = new AmqpValueDescribedType(content);

        testPeer.expectReceiverAttach();
        testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, amqpValueContent);
        testPeer.expectEnd();
        testPeer.expectClose();

        JMSConsumer messageConsumer = context.createConsumer(queue);
        try {
            messageConsumer.receiveBody(Boolean.class, 3000);
            fail("Should not read as Boolean type");
        } catch (MessageFormatRuntimeException mfre) {
        }

        context.close();

        testPeer.waitForAllHandlersToComplete(3000);
    }
}
 
Example 5
Source File: JMSConsumerIntegrationTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout=20000)
public void testNoReceivedMessagesWhenConnectionNotStarted() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        JMSContext context = testFixture.createJMSContext(testPeer);
        context.setAutoStart(false);

        testPeer.expectBegin();

        Queue destination = context.createQueue(getTestName());

        testPeer.expectReceiverAttach();
        testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, new AmqpValueDescribedType("content"), 3);
        testPeer.expectDispositionThatIsAcceptedAndSettled();

        JMSConsumer consumer = context.createConsumer(destination);

        assertNull(consumer.receive(100));

        context.start();

        assertNotNull(consumer.receive(2000));

        testPeer.expectEnd();
        testPeer.expectClose();
        context.close();

        testPeer.waitForAllHandlersToComplete(2000);
    }
}
 
Example 6
Source File: JMSConsumerIntegrationTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout=20000)
public void testConsumerReceiveNoWaitThrowsIfConnectionLost() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        JMSContext context = testFixture.createJMSContext(testPeer);

        testPeer.expectBegin();

        Queue queue = context.createQueue("queue");

        testPeer.expectReceiverAttach();
        testPeer.expectLinkFlow(false, notNullValue(UnsignedInteger.class));
        testPeer.expectLinkFlow(true, notNullValue(UnsignedInteger.class));
        testPeer.dropAfterLastHandler();

        final JMSConsumer consumer = context.createConsumer(queue);

        try {
            consumer.receiveNoWait();
            fail("An exception should have been thrown");
        } catch (JMSRuntimeException jmsre) {
            // Expected
        }

        try {
            context.close();
        } catch (Throwable ignored) {
        }
    }
}
 
Example 7
Source File: JMSConsumerIntegrationTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 20000)
public void testReceiveMessageWithReceiveZeroTimeout() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        JMSContext context = testFixture.createJMSContext(testPeer);

        testPeer.expectBegin();

        Queue queue = context.createQueue("myQueue");

        DescribedType amqpValueNullContent = new AmqpValueDescribedType(null);

        testPeer.expectReceiverAttach();
        testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, amqpValueNullContent);
        testPeer.expectDispositionThatIsAcceptedAndSettled();

        JMSConsumer messageConsumer = context.createConsumer(queue);
        Message receivedMessage = messageConsumer.receive(0);

        assertNotNull("A message should have been recieved", receivedMessage);

        testPeer.expectEnd();
        testPeer.expectClose();
        context.close();

        testPeer.waitForAllHandlersToComplete(2000);
    }
}
 
Example 8
Source File: JMSProducerIntegrationTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 20000)
public void testJMSProducerPropertyOverridesMessageValue() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        JMSContext context = testFixture.createJMSContext(testPeer, SERVER_ANONYMOUS_RELAY);
        testPeer.expectBegin();
        testPeer.expectSenderAttach();

        String queueName = "myQueue";
        Queue queue = context.createQueue(queueName);
        Message message = context.createMessage();
        JMSProducer producer = context.createProducer();

        ApplicationPropertiesSectionMatcher appPropsMatcher = new ApplicationPropertiesSectionMatcher(true);
        appPropsMatcher.withEntry(STRING_PROP, equalTo(STRING_PROP_VALUE));

        MessageHeaderSectionMatcher headersMatcher = new MessageHeaderSectionMatcher(true).withDurable(equalTo(true));
        MessageAnnotationsSectionMatcher msgAnnotationsMatcher = new MessageAnnotationsSectionMatcher(true);
        MessagePropertiesSectionMatcher propsMatcher = new MessagePropertiesSectionMatcher(true).withTo(equalTo(queueName));

        TransferPayloadCompositeMatcher messageMatcher = new TransferPayloadCompositeMatcher();
        messageMatcher.setHeadersMatcher(headersMatcher);
        messageMatcher.setMessageAnnotationsMatcher(msgAnnotationsMatcher);
        messageMatcher.setPropertiesMatcher(propsMatcher);
        messageMatcher.setApplicationPropertiesMatcher(appPropsMatcher);
        testPeer.expectTransfer(messageMatcher);

        message.setStringProperty(STRING_PROP, "ThisShouldNotBeTransmitted");
        producer.setProperty(STRING_PROP, STRING_PROP_VALUE);
        producer.send(queue, message);

        testPeer.expectEnd();
        testPeer.expectClose();

        context.close();

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
Example 9
Source File: ConsumerTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testContextOnConsumerAMQP() throws Throwable {
   if (!isNetty()) {
      // no need to run the test, there's no AMQP support
      return;
   }

   assertNull(server.getAddressInfo(SimpleString.toSimpleString("queue")));

   ConnectionFactory factory = createFactory(2);
   JMSContext context = factory.createContext("admin", "admin", Session.AUTO_ACKNOWLEDGE);

   try {
      javax.jms.Queue queue = context.createQueue("queue");

      JMSConsumer consumer = context.createConsumer(queue);

      ServerConsumer serverConsumer = null;
      for (ServerSession session : server.getSessions()) {
         for (ServerConsumer sessionConsumer : session.getServerConsumers()) {
            serverConsumer = sessionConsumer;
         }
      }

      consumer.close();

      Assert.assertTrue(serverConsumer.getProtocolContext() instanceof ProtonServerSenderContext);

      final AMQPSessionContext sessionContext = ((ProtonServerSenderContext)
         serverConsumer.getProtocolContext()).getSessionContext();

      Wait.assertEquals(0, () -> sessionContext.getSenderCount(), 1000, 10);
   } finally {
      context.stop();
      context.close();
   }
}
 
Example 10
Source File: JMSConsumerIntegrationTest.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 20000)
public void testReceiveBodyBytesMessage() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        JMSContext context = testFixture.createJMSContext(testPeer);

        testPeer.expectBegin();

        Queue queue = context.createQueue("myQueue");

        PropertiesDescribedType properties = new PropertiesDescribedType();
        properties.setContentType(AmqpMessageSupport.OCTET_STREAM_CONTENT_TYPE);

        MessageAnnotationsDescribedType msgAnnotations = null;
        msgAnnotations = new MessageAnnotationsDescribedType();
        msgAnnotations.setSymbolKeyedAnnotation(AmqpMessageSupport.JMS_MSG_TYPE.toString(), AmqpMessageSupport.JMS_BYTES_MESSAGE);

        final byte[] expectedContent = "expectedContent".getBytes();
        DescribedType dataContent = new DataDescribedType(new Binary(expectedContent));

        testPeer.expectReceiverAttach();
        testPeer.expectLinkFlowRespondWithTransfer(null, msgAnnotations, properties, null, dataContent);
        testPeer.expectDispositionThatIsAcceptedAndSettled();

        JMSConsumer messageConsumer = context.createConsumer(queue);
        byte[] received = messageConsumer.receiveBody(byte[].class, 3000);
        testPeer.waitForAllHandlersToComplete(3000);

        assertNotNull(received);
        assertTrue(Arrays.equals(expectedContent, received));

        testPeer.expectEnd();
        testPeer.expectClose();

        context.close();

        testPeer.waitForAllHandlersToComplete(3000);
    }
}
 
Example 11
Source File: DeliveryDelayTest.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private Destination createQueue(final JMSContext context, String queueName,
                                boolean holdsOnPublish) throws Exception
{
    Map<String, Object> attributes = new HashMap<>();
    attributes.put(org.apache.qpid.server.model.Queue.HOLD_ON_PUBLISH_ENABLED, holdsOnPublish);
    createEntityUsingAmqpManagement(queueName,
                                    "org.apache.qpid.Queue",
                                    attributes);
    return context.createQueue(queueName);
}
 
Example 12
Source File: JmsPoolJMSContextTest.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 30000)
public void testCreateConsumerWithSelector() {
    JMSContext context = cf.createContext();
    Queue queue = context.createQueue(getTestName());
    assertNotNull(context.createConsumer(queue, "color = red"));

    context.close();
    try {
        context.createConsumer(queue, "color = blue");
        fail("Should not be able to create resource when context is closed");
    } catch (IllegalStateRuntimeException isre) {}
}
 
Example 13
Source File: JmsPoolJMSContextTest.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 30000)
public void testCreateConsumer() {
    JMSContext context = cf.createContext();
    Queue queue = context.createQueue(getTestName());
    assertNotNull(context.createConsumer(queue));

    context.close();
    try {
        context.createConsumer(queue);
        fail("Should not be able to create resource when context is closed");
    } catch (IllegalStateRuntimeException isre) {}
}
 
Example 14
Source File: JmsPoolJMSContextTest.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 30000)
public void testCreateBrowserWithSelector() {
    JMSContext context = cf.createContext();
    Queue queue = context.createQueue(getTestName());
    assertNotNull(context.createBrowser(queue, "color = pink"));

    context.close();
    try {
        context.createBrowser(queue, "color = cyan");
        fail("Should not be able to create resource when context is closed");
    } catch (IllegalStateRuntimeException isre) {}
}
 
Example 15
Source File: JmsPoolJMSContextTest.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 30000)
public void testCreateBrowser() {
    JMSContext context = cf.createContext();
    Queue queue = context.createQueue(getTestName());
    assertNotNull(context.createBrowser(queue));

    context.close();
    try {
        context.createBrowser(queue);
        fail("Should not be able to create resource when context is closed");
    } catch (IllegalStateRuntimeException isre) {}
}
 
Example 16
Source File: JmsPoolJMSContextTest.java    From pooled-jms with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 30000)
public void testCreateQueue() {
    JMSContext context = cf.createContext();
    assertNotNull(context.createQueue(getTestName()));

    context.close();
    try {
        context.createQueue(getTestName());
        fail("Should not be able to create resource when context is closed");
    } catch (IllegalStateRuntimeException isre) {}
}
 
Example 17
Source File: JmsSource.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
private Destination getDestination(JMSContext context, String name, JmsConnectorIncomingConfiguration config) {
    String type = config.getDestinationType();
    switch (type.toLowerCase()) {
        case "queue":
            log.creatingQueue(name);
            return context.createQueue(name);
        case "topic":
            log.creatingTopic(name);
            return context.createTopic(name);
        default:
            throw ex.illegalArgumentUnknownDestinationType(type);
    }

}
 
Example 18
Source File: JMSConsumerIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
public void doTestReceiveBodyFailsThenCalledWithCorrectType(int sessionMode) throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        JMSContext context = testFixture.createJMSContext(testPeer);

        testPeer.expectBegin();

        final String content = "Message-Content";
        Queue queue = context.createQueue("myQueue");

        DescribedType amqpValueContent = new AmqpValueDescribedType(content);

        testPeer.expectReceiverAttach();
        testPeer.expectLinkFlowRespondWithTransfer(null, null, null, null, amqpValueContent);

        JMSConsumer messageConsumer = context.createConsumer(queue);
        try {
            messageConsumer.receiveBody(Boolean.class, 3000);
            fail("Should not read as Boolean type");
        } catch (MessageFormatRuntimeException mfre) {
        }

        testPeer.waitForAllHandlersToComplete(3000);

        if (sessionMode == JMSContext.AUTO_ACKNOWLEDGE ||
            sessionMode == JMSContext.DUPS_OK_ACKNOWLEDGE) {

            testPeer.expectDispositionThatIsAcceptedAndSettled();
        }

        String received = messageConsumer.receiveBody(String.class, 3000);

        if (sessionMode == JMSContext.AUTO_ACKNOWLEDGE ||
            sessionMode == JMSContext.DUPS_OK_ACKNOWLEDGE) {

            assertNotNull(received);
            assertEquals(content, received);
        } else {
            assertNull(received);
        }

        testPeer.expectEnd();
        testPeer.expectClose();

        context.close();

        testPeer.waitForAllHandlersToComplete(3000);
    }
}
 
Example 19
Source File: JMSConsumerIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 20000)
public void testReceiveBodyObjectMessage() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        JMSContext context = testFixture.createJMSContext(testPeer);

        testPeer.expectBegin();

        Queue queue = context.createQueue("myQueue");

        PropertiesDescribedType properties = new PropertiesDescribedType();
        properties.setContentType(AmqpMessageSupport.SERIALIZED_JAVA_OBJECT_CONTENT_TYPE);

        String expectedContent = "expectedContent";

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(expectedContent);
        oos.flush();
        oos.close();
        byte[] bytes = baos.toByteArray();

        MessageAnnotationsDescribedType msgAnnotations = new MessageAnnotationsDescribedType();
        msgAnnotations.setSymbolKeyedAnnotation(AmqpMessageSupport.JMS_MSG_TYPE.toString(), AmqpMessageSupport.JMS_OBJECT_MESSAGE);

        DescribedType dataContent = new DataDescribedType(new Binary(bytes));

        testPeer.expectReceiverAttach();
        testPeer.expectLinkFlowRespondWithTransfer(null, msgAnnotations, properties, null, dataContent);
        testPeer.expectDispositionThatIsAcceptedAndSettled();
        testPeer.expectEnd();
        testPeer.expectClose();

        JMSConsumer messageConsumer = context.createConsumer(queue);
        String received = messageConsumer.receiveBody(String.class, 3000);

        assertNotNull(received);
        assertEquals(expectedContent, received);

        context.close();

        testPeer.waitForAllHandlersToComplete(3000);
    }
}
 
Example 20
Source File: JMSConsumerIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 20000)
public void testReceiveBodyMapMessage() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        JMSContext context = testFixture.createJMSContext(testPeer);

        testPeer.expectBegin();

        Queue queue = context.createQueue("myQueue");

        // Prepare an AMQP message for the test peer to send, containing an
        // AmqpValue section holding a map with entries for each supported type,
        // and annotated as a JMS map message.
        String myBoolKey = "myBool";
        boolean myBool = true;
        String myByteKey = "myByte";
        byte myByte = 4;
        String myBytesKey = "myBytes";
        byte[] myBytes = myBytesKey.getBytes();
        String myCharKey = "myChar";
        char myChar = 'd';
        String myDoubleKey = "myDouble";
        double myDouble = 1234567890123456789.1234;
        String myFloatKey = "myFloat";
        float myFloat = 1.1F;
        String myIntKey = "myInt";
        int myInt = Integer.MAX_VALUE;
        String myLongKey = "myLong";
        long myLong = Long.MAX_VALUE;
        String myShortKey = "myShort";
        short myShort = 25;
        String myStringKey = "myString";
        String myString = myStringKey;

        Map<String, Object> map = new LinkedHashMap<String, Object>();
        map.put(myBoolKey, myBool);
        map.put(myByteKey, myByte);
        map.put(myBytesKey, new Binary(myBytes));// the underlying AMQP message uses Binary rather than byte[] directly.
        map.put(myCharKey, myChar);
        map.put(myDoubleKey, myDouble);
        map.put(myFloatKey, myFloat);
        map.put(myIntKey, myInt);
        map.put(myLongKey, myLong);
        map.put(myShortKey, myShort);
        map.put(myStringKey, myString);

        MessageAnnotationsDescribedType msgAnnotations = new MessageAnnotationsDescribedType();
        msgAnnotations.setSymbolKeyedAnnotation(AmqpMessageSupport.JMS_MSG_TYPE.toString(), AmqpMessageSupport.JMS_MAP_MESSAGE);

        DescribedType amqpValueSectionContent = new AmqpValueDescribedType(map);

        // receive the message from the test peer
        testPeer.expectReceiverAttach();
        testPeer.expectLinkFlowRespondWithTransfer(null, msgAnnotations, null, null, amqpValueSectionContent);
        testPeer.expectDispositionThatIsAcceptedAndSettled();
        testPeer.expectEnd();
        testPeer.expectClose();

        JMSConsumer messageConsumer = context.createConsumer(queue);
        @SuppressWarnings("unchecked")
        Map<String, Object> receivedMap = messageConsumer.receiveBody(Map.class, 3000);

        // verify the content is as expected
        assertNotNull("Map was not received", receivedMap);

        assertEquals("Unexpected boolean value", myBool, receivedMap.get(myBoolKey));
        assertEquals("Unexpected byte value", myByte, receivedMap.get(myByteKey));
        byte[] readBytes = (byte[]) receivedMap.get(myBytesKey);
        assertTrue("Read bytes were not as expected: " + Arrays.toString(readBytes), Arrays.equals(myBytes, readBytes));
        assertEquals("Unexpected char value", myChar, receivedMap.get(myCharKey));
        assertEquals("Unexpected double value", myDouble, (double) receivedMap.get(myDoubleKey), 0.0);
        assertEquals("Unexpected float value", myFloat, (float) receivedMap.get(myFloatKey), 0.0);
        assertEquals("Unexpected int value", myInt, receivedMap.get(myIntKey));
        assertEquals("Unexpected long value", myLong, receivedMap.get(myLongKey));
        assertEquals("Unexpected short value", myShort, receivedMap.get(myShortKey));
        assertEquals("Unexpected UTF value", myString, receivedMap.get(myStringKey));

        context.close();

        testPeer.waitForAllHandlersToComplete(3000);
    }
}