Java Code Examples for javax.jms.JMSContext#createConsumer()
The following examples show how to use
javax.jms.JMSContext#createConsumer() .
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: JMSContextTest.java From activemq-artemis with Apache License 2.0 | 6 votes |
@Test public void testJMSContextConsumerThrowsMessageFormatExceptionOnMalformedBody() throws Exception { Queue queue = createQueue(true, "ContextMalformedBodyTestQueue"); JMSContext context = qraConnectionFactory.createContext(); JMSProducer producer = context.createProducer(); TextMessage message = context.createTextMessage("TestMessage"); producer.send(queue, message); JMSConsumer consumer = context.createConsumer(queue); try { consumer.receiveBody(Boolean.class); fail("Should thrown MessageFormatException"); } catch (MessageFormatRuntimeException mfre) { // Do nothing test passed } catch (Exception e) { fail("Threw wrong exception, should be MessageFormatRuntimeException, instead got: " + e.getClass().getCanonicalName()); } }
Example 2
Source File: JMSConsumerIntegrationTest.java From qpid-jms with Apache License 2.0 | 6 votes |
@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: JMSConsumerIntegrationTest.java From qpid-jms with Apache License 2.0 | 5 votes |
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 4
Source File: JMSConsumerIntegrationTest.java From qpid-jms with Apache License 2.0 | 5 votes |
@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 5
Source File: JMSConsumerIntegrationTest.java From qpid-jms with Apache License 2.0 | 5 votes |
@Test(timeout = 20000) public void testReceiveBodyTextMessage() 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.expectDispositionThatIsAcceptedAndSettled(); testPeer.expectEnd(); testPeer.expectClose(); JMSConsumer messageConsumer = context.createConsumer(queue); String received = messageConsumer.receiveBody(String.class, 3000); assertNotNull(received); assertEquals(content, received); context.close(); testPeer.waitForAllHandlersToComplete(3000); } }
Example 6
Source File: JMSConsumerIntegrationTest.java From qpid-jms with Apache License 2.0 | 5 votes |
@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 |
@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: JMSConsumerIntegrationTest.java From qpid-jms with Apache License 2.0 | 5 votes |
@Test(timeout = 20000) public void testRemotelyCloseJMSConsumer() throws Exception { try (TestAmqpPeer testPeer = new TestAmqpPeer();) { JMSContext context = testFixture.createJMSContext(testPeer); testPeer.expectBegin(); // Create a consumer, then remotely end it afterwards. testPeer.expectReceiverAttach(); testPeer.expectLinkFlow(); testPeer.remotelyDetachLastOpenedLinkOnLastOpenedSession(true, true, AmqpError.RESOURCE_DELETED, "resource closed"); Queue queue = context.createQueue("myQueue"); final JMSConsumer consumer = context.createConsumer(queue); // Verify the consumer gets marked closed testPeer.waitForAllHandlersToComplete(1000); assertTrue("JMSConsumer never closed.", Wait.waitFor(new Wait.Condition() { @Override public boolean isSatisfied() throws Exception { try { consumer.getMessageListener(); } catch (IllegalStateRuntimeException jmsise) { return true; } return false; } }, 10000, 10)); // Try closing it explicitly, should effectively no-op in client. // The test peer will throw during close if it sends anything. consumer.close(); testPeer.expectEnd(); testPeer.expectClose(); context.close(); testPeer.waitForAllHandlersToComplete(1000); } }
Example 9
Source File: JmsContextTest.java From activemq-artemis with Apache License 2.0 | 5 votes |
@Test public void testCloseSecondContextConnectionRemainsOpen() throws JMSException { JMSContext localContext = context.createContext(JMSContext.CLIENT_ACKNOWLEDGE); Assert.assertEquals("client_ack", JMSContext.CLIENT_ACKNOWLEDGE, localContext.getSessionMode()); JMSProducer producer = localContext.createProducer(); JMSConsumer consumer = localContext.createConsumer(queue1); final int pass = 1; for (int idx = 0; idx < 2; idx++) { Message m = localContext.createMessage(); int intProperty = random.nextInt(); m.setIntProperty("random", intProperty); Assert.assertNotNull(m); producer.send(queue1, m); m = null; Message msg = consumer.receive(100); Assert.assertNotNull("must have a msg", msg); Assert.assertEquals(intProperty, msg.getIntProperty("random")); /* In the second pass we close the connection before ack'ing */ if (idx == pass) { localContext.close(); } /** * From {@code JMSContext.close()}'s javadoc:<br/> * Invoking the {@code acknowledge} method of a received message from a closed connection's * session must throw an {@code IllegalStateRuntimeException}. Closing a closed connection * must NOT throw an exception. */ try { msg.acknowledge(); Assert.assertEquals("connection should be open on pass 0. It is " + pass, 0, idx); } catch (javax.jms.IllegalStateException expected) { // HORNETQ-1209 "JMS 2.0" XXX JMSContext javadoc says we must expect a // IllegalStateRuntimeException here. But Message.ack...() says it must throws the // non-runtime variant. Assert.assertEquals("we only close the connection on pass " + pass, pass, idx); } } }
Example 10
Source File: ConsumerTest.java From activemq-artemis with Apache License 2.0 | 5 votes |
@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 11
Source File: JMSSessionFactory.java From apicurio-studio with Apache License 2.0 | 5 votes |
/** * Called to add a consumer to the JMS topic specific to the given design id. * @param designId * @param handler */ public synchronized MessagingSessionContainer joinSession(String designId, IOperationHandler handler) { logger.debug("Joining session {}", designId); JMSContext context = connectionFactory.createContext(); Topic sessionTopic = context.createTopic(JAVA_JMS_TOPIC_SESSION + designId); // Subscribe to the topic JMSConsumer consumer = context.createConsumer(sessionTopic, null, true); // When a new node joins the distributed session, it doesn't know about the session(s) attached to the // other nodes already in the session(s). return new MessagingSessionContainer(sessionTopic, consumer, context.createProducer(), handler); }
Example 12
Source File: JmsPoolJMSContextTest.java From pooled-jms with Apache License 2.0 | 5 votes |
@Test(timeout = 30000) public void testCreateConsumerWithSelectorAndNoLocal() { JMSContext context = cf.createContext(); Queue queue = context.createQueue(getTestName()); assertNotNull(context.createConsumer(queue, "color = red", false)); context.close(); try { context.createConsumer(queue, "color = blue", true); 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 |
@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 14
Source File: JmsPoolJMSContextTest.java From pooled-jms with Apache License 2.0 | 5 votes |
@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 15
Source File: JmsSource.java From smallrye-reactive-messaging with Apache License 2.0 | 5 votes |
JmsSource(JMSContext context, JmsConnectorIncomingConfiguration config, Jsonb json, Executor executor) { String name = config.getDestination().orElseGet(config::getChannel); String selector = config.getSelector().orElse(null); boolean nolocal = config.getNoLocal(); boolean broadcast = config.getBroadcast(); boolean durable = config.getDurable(); Destination destination = getDestination(context, name, config); JMSConsumer consumer; if (durable) { if (!(destination instanceof Topic)) { throw ex.illegalArgumentInvalidDestination(); } consumer = context.createDurableConsumer((Topic) destination, name, selector, nolocal); } else { consumer = context.createConsumer(destination, selector, nolocal); } publisher = new JmsPublisher(consumer); if (!broadcast) { source = ReactiveStreams.fromPublisher(publisher).map(m -> new IncomingJmsMessage<>(m, executor, json)); } else { source = ReactiveStreams.fromPublisher( Multi.createFrom().publisher(publisher) .map(m -> new IncomingJmsMessage<>(m, executor, json)) .broadcast().toAllSubscribers()); } }
Example 16
Source File: JMSConsumerIntegrationTest.java From qpid-jms with Apache License 2.0 | 4 votes |
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 17
Source File: JMSConsumerIntegrationTest.java From qpid-jms with Apache License 2.0 | 4 votes |
@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); } }
Example 18
Source File: JMSConsumerIntegrationTest.java From qpid-jms with Apache License 2.0 | 4 votes |
@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 19
Source File: JmsContextTest.java From activemq-artemis with Apache License 2.0 | 3 votes |
@Test public void testRollbackTest() { JMSContext ctx = addContext(cf.createContext(JMSContext.SESSION_TRANSACTED)); JMSProducer producer = ctx.createProducer(); JMSConsumer cons = ctx.createConsumer(queue1); producer.send(queue1, context.createTextMessage("hello")); ctx.rollback(); assertNull(cons.receiveNoWait()); producer.send(queue1, context.createTextMessage("hello")); ctx.commit(); assertNotNull(cons.receiveNoWait()); ctx.commit(); ctx.rollback(); assertNull(cons.receiveNoWait()); cons.close(); }
Example 20
Source File: JmsContextTest.java From activemq-artemis with Apache License 2.0 | 2 votes |
@Test public void testContextStopAndCloseFromMessageListeners() throws Exception { final JMSContext context1 = context.createContext(Session.AUTO_ACKNOWLEDGE); JMSConsumer consumer1 = context1.createConsumer(queue1); final CountDownLatch latch1 = new CountDownLatch(1); InvalidMessageListener listener1 = new InvalidMessageListener(context1, latch1, 1); consumer1.setMessageListener(listener1); JMSProducer producer = context1.createProducer(); Message msg = context1.createTextMessage("first message"); producer.send(queue1, msg); latch1.await(); Throwable error1 = listener1.getError(); assertNotNull(error1); assertTrue(error1 instanceof IllegalStateRuntimeException); context1.close(); final JMSContext context2 = context.createContext(Session.AUTO_ACKNOWLEDGE); JMSConsumer consumer2 = context2.createConsumer(queue1); final CountDownLatch latch2 = new CountDownLatch(1); InvalidMessageListener listener2 = new InvalidMessageListener(context2, latch2, 2); consumer2.setMessageListener(listener2); JMSProducer producer2 = context2.createProducer(); Message msg2 = context2.createTextMessage("second message"); producer2.send(queue1, msg2); latch2.await(); Throwable error2 = listener2.getError(); assertNotNull(error2); assertTrue(error2 instanceof IllegalStateRuntimeException); context2.close(); }