private void assertTextMessage(MessageCreator messageCreator) { try { TextMessage jmsMessage = createTextMessage(messageCreator); assertEquals("Wrong body message", "Hello", jmsMessage.getText()); assertEquals("Invalid foo property", "bar", jmsMessage.getStringProperty("foo")); } catch (JMSException e) { throw new IllegalStateException("Wrong text message", e); } }
@Test public void fromTextMessageAsMap() throws Exception { TextMessage textMessageMock = mock(TextMessage.class); Map<String, String> unmarshalled = Collections.singletonMap("foo", "bar"); String text = "{\"foo\":\"bar\"}"; given(textMessageMock.getStringProperty("__typeid__")).willReturn(HashMap.class.getName()); given(textMessageMock.getText()).willReturn(text); Object result = converter.fromMessage(textMessageMock); assertEquals("Invalid result", result, unmarshalled); }
@Test public void fromTextMessageAsObject() throws Exception { TextMessage textMessageMock = mock(TextMessage.class); Map<String, String> unmarshalled = Collections.singletonMap("foo", "bar"); String text = "{\"foo\":\"bar\"}"; given(textMessageMock.getStringProperty("__typeid__")).willReturn(Object.class.getName()); given(textMessageMock.getText()).willReturn(text); Object result = converter.fromMessage(textMessageMock); assertEquals("Invalid result", result, unmarshalled); }
@Test public void fromTextMessage() throws Exception { TextMessage textMessageMock = mock(TextMessage.class); MyBean unmarshalled = new MyBean("bar"); String text = "{\"foo\":\"bar\"}"; given(textMessageMock.getStringProperty("__typeid__")).willReturn(MyBean.class.getName()); given(textMessageMock.getText()).willReturn(text); MyBean result = (MyBean)converter.fromMessage(textMessageMock); assertEquals("Invalid result", result, unmarshalled); }
@Test public void fromTextMessageWithUnknownProperty() throws Exception { TextMessage textMessageMock = mock(TextMessage.class); MyBean unmarshalled = new MyBean("bar"); String text = "{\"foo\":\"bar\", \"unknownProperty\":\"value\"}"; given(textMessageMock.getStringProperty("__typeid__")).willReturn(MyBean.class.getName()); given(textMessageMock.getText()).willReturn(text); MyBean result = (MyBean)converter.fromMessage(textMessageMock); assertEquals("Invalid result", result, unmarshalled); }
Queue destination = session.createQueue("your_q"); QueueBrowser browser = session.createBrowser(destination); Enumeration<?> enum1 = browser.getEnumeration(); while(enum1.hasMoreElements()) { TextMessage msg = (TextMessage)enum1.nextElement(); if(msg.getStringProperty("any_prop").equals("some_prop")) { MessageConsumer consumer = session.createConsumer(destination, "id='" + msg.getStringProperty("id") + "'"); consumer.receive(1000); } }
@Override public void onMessage(Message message) { try { count++; TextMessage msg = (TextMessage) message; System.out.println("Message Received " + msg.getText()); System.out.println("Message received from " + msg.getStringProperty("SentToNode")); //now you can send the message to that node } catch (JMSException e) { e.printStackTrace(); } }
private void receiveLVQ(ConnectionSupplier consumerConnectionSupplier, String queueName, String lastValueKey) throws JMSException { try (Connection connection = consumerConnectionSupplier.createConnection(); Session session = connection.createSession(); MessageConsumer consumer = session.createConsumer(session.createQueue(queueName))) { TextMessage msg = (TextMessage) consumer.receive(1000); assertNotNull(msg); assertEquals("KEY", msg.getStringProperty(lastValueKey)); assertEquals("how are you", msg.getText()); } }
private void receiveLVQTombstone(ConnectionSupplier consumerConnectionSupplier, String queueName, String lastValueKey) throws JMSException { try (Connection connection = consumerConnectionSupplier.createConnection(); Session session = connection.createSession(); MessageConsumer consumer = session.createConsumer(session.createQueue(queueName))) { TextMessage msg = (TextMessage) consumer.receive(1000); assertNotNull(msg); assertEquals("KEY", msg.getStringProperty(lastValueKey)); assertEquals("tombstone", msg.getText()); } }
private void receiveLVQ(ConnectionSupplier consumerConnectionSupplier, String queueName, String lastValueKey) throws JMSException { try (Connection consumerConnection = consumerConnectionSupplier.createConnection()) { Session consumerSession = consumerConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); Queue consumerQueue = consumerSession.createQueue(queueName); MessageConsumer consumer = consumerSession.createConsumer(consumerQueue); TextMessage msg = (TextMessage) consumer.receive(1000); assertNotNull(msg); assertEquals("KEY", msg.getStringProperty(lastValueKey)); assertEquals("how are you", msg.getText()); consumer.close(); } }
@Test public void testReceiveContentType() throws Exception { MessageConsumer consumer = session.createConsumer(queue); conn.connect(defUser, defPass); ClientStompFrame response = send(conn, getQueuePrefix() + getQueueName(), "text/plain", "Hello World"); TextMessage message = (TextMessage) consumer.receive(1000); Assert.assertNotNull(message); Assert.assertEquals("text/plain", message.getStringProperty(org.apache.activemq.artemis.api.core.Message.HDR_CONTENT_TYPE.toString())); }
@Test public void testSendMessageWithCustomHeadersAndHyphenatedSelector() throws Exception { MessageConsumer consumer = session.createConsumer(queue, "hyphenated_props:b-ar = '123'"); conn.connect(defUser, defPass); ClientStompFrame frame = conn.createFrame(Stomp.Commands.SEND) .addHeader(Stomp.Headers.Send.DESTINATION, getQueuePrefix() + getQueueName()) .addHeader("foo", "abc") .addHeader("b-ar", "123") .setBody("Hello World"); conn.sendFrame(frame); TextMessage message = (TextMessage) consumer.receive(1000); Assert.assertNotNull(message); Assert.assertEquals("Hello World", message.getText()); Assert.assertEquals("foo", "abc", message.getStringProperty("foo")); Assert.assertEquals("b-ar", "123", message.getStringProperty("b-ar")); }
@Test public void testSendMessageWithCustomHeadersAndSelector() throws Exception { MessageConsumer consumer = session.createConsumer(queue, "foo = 'abc'"); conn.connect(defUser, defPass); ClientStompFrame frame = conn.createFrame(Stomp.Commands.SEND) .addHeader(Stomp.Headers.Send.DESTINATION, getQueuePrefix() + getQueueName()) .addHeader("foo", "abc") .addHeader("bar", "123") .setBody("Hello World"); conn.sendFrame(frame); TextMessage message = (TextMessage) consumer.receive(1000); Assert.assertNotNull(message); Assert.assertEquals("Hello World", message.getText()); Assert.assertEquals("foo", "abc", message.getStringProperty("foo")); Assert.assertEquals("bar", "123", message.getStringProperty("bar")); }
@Test public void testSendMessageWithCustomHeadersAndSelector() throws Exception { MessageConsumer consumer = session.createConsumer(queue, "foo = 'abc'"); conn.connect(defUser, defPass); ClientStompFrame frame = conn.createFrame(Stomp.Commands.SEND) .addHeader("foo", "abc") .addHeader("bar", "123") .addHeader(Stomp.Headers.Send.DESTINATION, getQueuePrefix() + getQueueName()) .setBody("Hello World"); conn.sendFrame(frame); TextMessage message = (TextMessage) consumer.receive(1000); Assert.assertNotNull(message); Assert.assertEquals("Hello World", message.getText()); Assert.assertEquals("foo", "abc", message.getStringProperty("foo")); Assert.assertEquals("bar", "123", message.getStringProperty("bar")); }
@Test public void testSendMessageWithCustomHeadersAndSelector() throws Exception { MessageConsumer consumer = session.createConsumer(queue, "foo = 'abc'"); conn.connect(defUser, defPass); ClientStompFrame frame = conn.createFrame(Stomp.Commands.SEND) .addHeader(Stomp.Headers.Subscribe.DESTINATION, getQueuePrefix() + getQueueName()) .addHeader("foo", "abc") .addHeader("bar", "123") .setBody("Hello World"); conn.sendFrame(frame); TextMessage message = (TextMessage) consumer.receive(1000); Assert.assertNotNull(message); Assert.assertEquals("Hello World", message.getText()); Assert.assertEquals("foo", "abc", message.getStringProperty("foo")); Assert.assertEquals("bar", "123", message.getStringProperty("bar")); }
@Test public void testJMSXGroupIdCanBeSet() throws Exception { MessageConsumer consumer = session.createConsumer(queue); conn.connect(defUser, defPass); ClientStompFrame frame = conn.createFrame(Stomp.Commands.SEND) .addHeader(Stomp.Headers.Send.DESTINATION, getQueuePrefix() + getQueueName()) .addHeader("JMSXGroupID", "TEST") .setBody("Hello World"); conn.sendFrame(frame); TextMessage message = (TextMessage) consumer.receive(1000); Assert.assertNotNull(message); Assert.assertEquals("Hello World", message.getText()); // differ from StompConnect Assert.assertEquals("TEST", message.getStringProperty("JMSXGroupID")); }
@Test public void testJMSXGroupIdCanBeSet() throws Exception { final String jmsxGroupID = "JMSXGroupID"; MessageConsumer consumer = session.createConsumer(queue); conn.connect(defUser, defPass); ClientStompFrame frame = conn.createFrame(Stomp.Commands.SEND) .addHeader(Stomp.Headers.Send.DESTINATION, getQueuePrefix() + getQueueName()) .addHeader("JMSXGroupID", jmsxGroupID) .setBody("Hello World"); conn.sendFrame(frame); TextMessage message = (TextMessage) consumer.receive(1000); Assert.assertNotNull(message); Assert.assertEquals("Hello World", message.getText()); // differ from StompConnect Assert.assertEquals(jmsxGroupID, message.getStringProperty("JMSXGroupID")); }
@Test public void testJMSXGroupIdCanBeSet() throws Exception { MessageConsumer consumer = session.createConsumer(queue); conn.connect(defUser, defPass); ClientStompFrame frame = conn.createFrame(Stomp.Commands.SEND); frame.addHeader(Stomp.Headers.Subscribe.DESTINATION, getQueuePrefix() + getQueueName()); frame.addHeader("JMSXGroupID", "TEST"); frame.setBody("Hello World"); conn.sendFrame(frame); TextMessage message = (TextMessage) consumer.receive(1000); Assert.assertNotNull(message); Assert.assertEquals("Hello World", message.getText()); // differ from StompConnect Assert.assertEquals("TEST", message.getStringProperty("JMSXGroupID")); }
/** * Test that the <code>TextMessage.clearBody()</code> method does not clear the * message properties. */ @Test public void testClearBody_2() { try { TextMessage message = senderSession.createTextMessage(); message.setStringProperty("prop", "foo"); message.clearBody(); Assert.assertEquals("sec. 3.11.1 Clearing a message's body does not clear its property entries.\n", "foo", message.getStringProperty("prop")); } catch (JMSException e) { fail(e); } }
/** * Test that the <code>Message.clearProperties()</code> method deletes all the * properties of the Message. */ @Test public void testClearProperties_1() { try { TextMessage message = senderSession.createTextMessage(); message.setStringProperty("prop", "foo"); message.clearProperties(); Assert.assertEquals("sec. 3.5.7 A message's properties are deleted by the clearProperties method.\n", null, message.getStringProperty("prop")); } catch (JMSException e) { fail(e); } } }