Java Code Examples for javax.jms.MapMessage#setInt()

The following examples show how to use javax.jms.MapMessage#setInt() . 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: JMSUtilsTest.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * Test ConvertJMSMapToXML
 *
 * @throws Exception
 */
@Test
public void testConvertJMSMapToXML() throws Exception {
    String queueName = "testHandler";
    Properties jmsProperties = JMSTestsUtils.getJMSPropertiesForDestination(queueName, PROVIDER_URL, false);
    JMSBrokerController brokerController = new JMSBrokerController(PROVIDER_URL, jmsProperties);
    try {
        brokerController.startProcess();
        brokerController.connect(queueName, true);
        MapMessage mapMessage = brokerController.createMapMessage();
        mapMessage.setStringProperty("MessageFormat", "Person");
        mapMessage.setString("NAME", queueName);
        mapMessage.setInt("COUNT", 10);
        mapMessage.setDouble("PRICE", 12.00);
        OMElement result = JMSInjectHandler.convertJMSMapToXML(mapMessage);
        Assert.assertEquals("The converted XML is not correct", "10", ((OMElement) result.
                getChildrenWithLocalName("COUNT").next()).getText());
        Assert.assertEquals("The converted XML is not correct", queueName, ((OMElement) result.
                getChildrenWithLocalName("NAME").next()).getText());
    } finally {
        brokerController.stopProcess();
    }
}
 
Example 2
Source File: MessageOrderingTest.java    From olat with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
    connectionFactory.setBrokerURL("tcp://localhost:61616");

    Connection connection = connectionFactory.createQueueConnection();
    Session session1 = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Session session2 = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    Destination destination = new ActiveMQQueue("/atestqueue");

    MessageProducer producer = session1.createProducer(destination);

    MessageConsumer consumer = session2.createConsumer(destination);

    consumer.setMessageListener(new MessageOrderingTest());
    connection.start();

    for (int i = 0; i < 10000; i++) {
        MapMessage message = session1.createMapMessage();
        message.setInt("Counter", i);
        producer.send(message);
        System.out.println("Sent counter=" + i);
    }
}
 
Example 3
Source File: CompressedInteropTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private void sendCompressedMapMessageUsingOpenWire() throws Exception {
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   ActiveMQDestination destination = createDestination(session, ActiveMQDestination.QUEUE_TYPE);

   final ActiveMQMessageProducer producer = (ActiveMQMessageProducer) session.createProducer(destination);

   MapMessage mapMessage = session.createMapMessage();

   mapMessage.setBoolean("boolean-type", true);
   mapMessage.setByte("byte-type", (byte) 10);
   mapMessage.setBytes("bytes-type", TEXT.getBytes());
   mapMessage.setChar("char-type", 'A');
   mapMessage.setDouble("double-type", 55.3D);
   mapMessage.setFloat("float-type", 79.1F);
   mapMessage.setInt("int-type", 37);
   mapMessage.setLong("long-type", 56652L);
   mapMessage.setObject("object-type", new String("VVVV"));
   mapMessage.setShort("short-type", (short) 333);
   mapMessage.setString("string-type", TEXT);

   producer.send(mapMessage);
}
 
Example 4
Source File: MapMessageTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
@Override
protected void prepareMessage(final Message m) throws JMSException {
   super.prepareMessage(m);

   MapMessage mm = (MapMessage) m;

   mm.setBoolean("boolean", true);
   mm.setByte("byte", (byte) 3);
   mm.setBytes("bytes", new byte[]{(byte) 3, (byte) 4, (byte) 5});
   mm.setChar("char", (char) 6);
   mm.setDouble("double", 7.0);
   mm.setFloat("float", 8.0f);
   mm.setInt("int", 9);
   mm.setLong("long", 10L);
   mm.setObject("object", new String("this is an object"));
   mm.setShort("short", (short) 11);
   mm.setString("string", "this is a string");
}
 
Example 5
Source File: ConsumeJMSManualTest.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testMapMessage() throws Exception {
    MessageCreator messageCreator = session -> {
        MapMessage message = session.createMapMessage();

        message.setBoolean("boolean", true);
        message.setByte("byte", Integer.valueOf(1).byteValue());
        message.setBytes("bytes", new byte[] {2, 3, 4});
        message.setShort("short", (short)32);
        message.setInt("int", 64);
        message.setLong("long", 128L);
        message.setFloat("float", 1.25F);
        message.setDouble("double", 100.867);
        message.setChar("char", 'c');
        message.setString("string", "someString");
        message.setObject("object", "stringAsObject");

        return message;
    };

    send(messageCreator);
}
 
Example 6
Source File: MessageCompressionTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private void sendTestMapMessage(ActiveMQConnectionFactory factory, String message) throws JMSException {
   ActiveMQConnection connection = (ActiveMQConnection) factory.createConnection();
   Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
   MessageProducer producer = session.createProducer(queue);
   MapMessage mapMessage = session.createMapMessage();

   mapMessage.setBoolean("boolean-type", true);
   mapMessage.setByte("byte-type", (byte) 10);
   mapMessage.setBytes("bytes-type", TEXT.getBytes());
   mapMessage.setChar("char-type", 'A');
   mapMessage.setDouble("double-type", 55.3D);
   mapMessage.setFloat("float-type", 79.1F);
   mapMessage.setInt("int-type", 37);
   mapMessage.setLong("long-type", 56652L);
   mapMessage.setObject("object-type", new String("VVVV"));
   mapMessage.setShort("short-type", (short) 333);
   mapMessage.setString("string-type", TEXT);

   producer.send(mapMessage);
   connection.close();
}
 
Example 7
Source File: TestJmsConsumer.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
static protected MapMessage createMapMessage() throws JMSException {
    MapMessage mapMessage = new ActiveMQMapMessage();
    mapMessage.setString("name", "Arnold");
    mapMessage.setInt("age", 97);
    mapMessage.setDouble("xyz", 89686.564);
    mapMessage.setBoolean("good", true);
    return mapMessage;
}
 
Example 8
Source File: JMSProducerImpl.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public JMSProducer send(final Destination destination, final Map<String, Object> body) {
    final MapMessage message = wrap(context.createMapMessage());
    if (body != null) {
        try {
            for (final Map.Entry<String, Object> entry : body.entrySet()) {
                final String name = entry.getKey();
                final Object v = entry.getValue();
                if (v instanceof String) {
                    message.setString(name, (String) v);
                } else if (v instanceof Long) {
                    message.setLong(name, (Long) v);
                } else if (v instanceof Double) {
                    message.setDouble(name, (Double) v);
                } else if (v instanceof Integer) {
                    message.setInt(name, (Integer) v);
                } else if (v instanceof Character) {
                    message.setChar(name, (Character) v);
                } else if (v instanceof Short) {
                    message.setShort(name, (Short) v);
                } else if (v instanceof Boolean) {
                    message.setBoolean(name, (Boolean) v);
                } else if (v instanceof Float) {
                    message.setFloat(name, (Float) v);
                } else if (v instanceof Byte) {
                    message.setByte(name, (Byte) v);
                } else if (v instanceof byte[]) {
                    byte[] array = (byte[]) v;
                    message.setBytes(name, array, 0, array.length);
                } else {
                    message.setObject(name, v);
                }
            }
        } catch (final JMSException e) {
            throw new MessageFormatRuntimeException(e.getMessage());
        }
    }
    send(destination, message);
    return this;
}
 
Example 9
Source File: ActiveMQJMSProducer.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Override
public JMSProducer send(Destination destination, Map<String, Object> body) {
   MapMessage message = context.createMapMessage();
   if (body != null) {
      try {
         for (Entry<String, Object> entry : body.entrySet()) {
            final String name = entry.getKey();
            final Object v = entry.getValue();
            if (v instanceof String) {
               message.setString(name, (String) v);
            } else if (v instanceof Long) {
               message.setLong(name, (Long) v);
            } else if (v instanceof Double) {
               message.setDouble(name, (Double) v);
            } else if (v instanceof Integer) {
               message.setInt(name, (Integer) v);
            } else if (v instanceof Character) {
               message.setChar(name, (Character) v);
            } else if (v instanceof Short) {
               message.setShort(name, (Short) v);
            } else if (v instanceof Boolean) {
               message.setBoolean(name, (Boolean) v);
            } else if (v instanceof Float) {
               message.setFloat(name, (Float) v);
            } else if (v instanceof Byte) {
               message.setByte(name, (Byte) v);
            } else if (v instanceof byte[]) {
               byte[] array = (byte[]) v;
               message.setBytes(name, array, 0, array.length);
            } else {
               message.setObject(name, v);
            }
         }
      } catch (JMSException e) {
         throw new MessageFormatRuntimeException(e.getMessage());
      }
   }
   send(destination, message);
   return this;
}
 
Example 10
Source File: MessageHeaderTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testCopyOnForeignMapMessage() throws JMSException {
   ClientMessage clientMessage = new ClientMessageImpl(ActiveMQTextMessage.TYPE, true, 0, System.currentTimeMillis(), (byte) 4, 1000);
   ClientSession session = new FakeSession(clientMessage);
   MapMessage foreignMapMessage = new SimpleJMSMapMessage();
   foreignMapMessage.setInt("int", 1);
   foreignMapMessage.setString("string", "test");

   ActiveMQMapMessage copy = new ActiveMQMapMessage(foreignMapMessage, session);

   MessageHeaderTestBase.ensureEquivalent(foreignMapMessage, copy);
}
 
Example 11
Source File: InactiveDurableTopicTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public void test2ProducerTestCase() {
     /*
      * Step 2 - Establish a connection without a client id and create a
      * producer and start pumping messages. We will get hung
      */
   try {
      connection = connectionFactory.createConnection(USERNAME, DEFAULT_PASSWORD);
      assertNotNull(connection);
      session = connection.createSession(false, javax.jms.Session.CLIENT_ACKNOWLEDGE);
      assertNotNull(session);
      topic = session.createTopic(TOPIC_NAME);
      assertNotNull(topic);
      publisher = session.createProducer(topic);
      assertNotNull(publisher);
      MapMessage msg = session.createMapMessage();
      assertNotNull(msg);
      msg.setString("key1", "value1");
      int loop;
      for (loop = 0; loop < MESSAGE_COUNT; loop++) {
         msg.setInt("key2", loop);
         publisher.send(msg, DELIVERY_MODE, DELIVERY_PRIORITY, Message.DEFAULT_TIME_TO_LIVE);
         if (loop % 5000 == 0) {
            LOG.info("Sent " + loop + " messages");
         }
      }
      assertEquals(loop, MESSAGE_COUNT);
      publisher.close();
      session.close();
      connection.stop();
      connection.stop();
   } catch (JMSException ex) {
      try {
         connection.close();
      } catch (Exception ignore) {
      }
      throw new AssertionFailedError("Create Subscription caught: " + ex);
   }
}
 
Example 12
Source File: InactiveQueueTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public void testNoSubscribers() throws Exception {
   connection = connectionFactory.createConnection(USERNAME, DEFAULT_PASSWORD);
   assertNotNull(connection);
   connection.start();
   session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
   assertNotNull(session);
   destination = session.createQueue(QUEUE_NAME);
   assertNotNull(destination);
   publisher = session.createProducer(destination);
   assertNotNull(publisher);
   MapMessage msg = session.createMapMessage();
   assertNotNull(msg);
   msg.setString("key1", "value1");
   int loop;
   for (loop = 0; loop < MESSAGE_COUNT; loop++) {
      msg.setInt("key2", loop);
      publisher.send(msg, DELIVERY_MODE, DELIVERY_PRIORITY, Message.DEFAULT_TIME_TO_LIVE);
      if (loop % 500 == 0) {
         LOG.debug("Sent " + loop + " messages");
      }
   }
   Thread.sleep(1000000);
   assertEquals(loop, MESSAGE_COUNT);
   publisher.close();
   session.close();
   connection.stop();
   connection.stop();
}
 
Example 13
Source File: JMSPublisherConsumerIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Test
public void testMapMessage() throws Exception {
    final String destinationName = "testObjectMessage";

    MessageCreator messageCreator = session -> {
        MapMessage message = session.createMapMessage();

        message.setBoolean("boolean", true);
        message.setByte("byte", Integer.valueOf(1).byteValue());
        message.setBytes("bytes", new byte[] {2, 3, 4});
        message.setShort("short", (short)32);
        message.setInt("int", 64);
        message.setLong("long", 128L);
        message.setFloat("float", 1.25F);
        message.setDouble("double", 100.867);
        message.setChar("char", 'c');
        message.setString("string", "someString");
        message.setObject("object", "stringAsObject");

        return message;
    };

    String expectedJson = "{" +
        "\"boolean\":true," +
        "\"byte\":1," +
        "\"bytes\":[2, 3, 4]," +
        "\"short\":32," +
        "\"int\":64," +
        "\"long\":128," +
        "\"float\":1.25," +
        "\"double\":100.867," +
        "\"char\":\"c\"," +
        "\"string\":\"someString\"," +
        "\"object\":\"stringAsObject\"" +
        "}";

    testMapMessage(destinationName, messageCreator, expectedJson);
}
 
Example 14
Source File: ReSendMessageTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testResendWithMapMessagesOnly() throws Exception {
   conn = cf.createConnection();
   conn.start();

   Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
   ArrayList<Message> msgs = new ArrayList<>();

   for (int i = 0; i < 1; i++) {
      MapMessage mm = sess.createMapMessage();
      mm.setBoolean("boolean", true);
      mm.setByte("byte", (byte) 3);
      mm.setBytes("bytes", new byte[]{(byte) 3, (byte) 4, (byte) 5});
      mm.setChar("char", (char) 6);
      mm.setDouble("double", 7.0);
      mm.setFloat("float", 8.0f);
      mm.setInt("int", 9);
      mm.setLong("long", 10L);
      mm.setObject("object", new String("this is an object"));
      mm.setShort("short", (short) 11);
      mm.setString("string", "this is a string");

      msgs.add(mm);

      MapMessage emptyMap = sess.createMapMessage();
      msgs.add(emptyMap);
   }

   internalTestResend(msgs, sess);
}
 
Example 15
Source File: ReSendMessageTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
@Test
public void testResendWithLargeMessage() throws Exception {
   conn = cf.createConnection();
   conn.start();

   Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);
   ArrayList<Message> msgs = new ArrayList<>();

   for (int i = 0; i < 10; i++) {
      BytesMessage bm = sess.createBytesMessage();
      bm.setObjectProperty(ActiveMQJMSConstants.JMS_ACTIVEMQ_INPUT_STREAM, ActiveMQTestBase.createFakeLargeStream(2 * ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE));
      msgs.add(bm);

      MapMessage mm = sess.createMapMessage();
      mm.setBoolean("boolean", true);
      mm.setByte("byte", (byte) 3);
      mm.setBytes("bytes", new byte[]{(byte) 3, (byte) 4, (byte) 5});
      mm.setChar("char", (char) 6);
      mm.setDouble("double", 7.0);
      mm.setFloat("float", 8.0f);
      mm.setInt("int", 9);
      mm.setLong("long", 10L);
      mm.setObject("object", new String("this is an object"));
      mm.setShort("short", (short) 11);
      mm.setString("string", "this is a string");

      msgs.add(mm);
      msgs.add(sess.createTextMessage("hello" + i));
      msgs.add(sess.createObjectMessage(new SomeSerializable("hello" + i)));
   }

   internalTestResend(msgs, sess);
}
 
Example 16
Source File: MapMessageTest.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
private void setMapValues(MapMessage message) throws JMSException
{
    message.setBoolean("bool", true);
    message.setByte("byte",Byte.MAX_VALUE);
    message.setBytes("bytes", BYTES);
    message.setChar("char",'c');
    message.setDouble("double", Double.MAX_VALUE);
    message.setFloat("float", Float.MAX_VALUE);
    message.setFloat("smallfloat", SMALL_FLOAT);
    message.setInt("int",  Integer.MAX_VALUE);
    message.setLong("long",  Long.MAX_VALUE);
    message.setShort("short", Short.MAX_VALUE);
    message.setString("string-ascii", MESSAGE_ASCII);
    message.setString("string-utf8", MESSAGE_NON_ASCII_UTF8);

    // Test Setting Object Values
    message.setObject("object-bool", true);
    message.setObject("object-byte", Byte.MAX_VALUE);
    message.setObject("object-bytes", BYTES);
    message.setObject("object-char", 'c');
    message.setObject("object-double", Double.MAX_VALUE);
    message.setObject("object-float", Float.MAX_VALUE);
    message.setObject("object-int", Integer.MAX_VALUE);
    message.setObject("object-long", Long.MAX_VALUE);
    message.setObject("object-short", Short.MAX_VALUE);

    // Set a null String value
    message.setString("nullString", null);
    // Highlight protocol problem
    message.setString("emptyString", "");
}
 
Example 17
Source File: Retailer.java    From chipster with MIT License 4 votes vote down vote up
public void run() {
	ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
	try {
		Connection connection = connectionFactory.createConnection();
		
		// The Retailer's session is non-trasacted.
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Destination vendorOrderQueue = session.createQueue("VendorOrderQueue");
		TemporaryQueue retailerConfirmQueue = session.createTemporaryQueue();
		
		MessageProducer producer = session.createProducer(vendorOrderQueue);
		MessageConsumer replyConsumer = session.createConsumer(retailerConfirmQueue);

		connection.start();

		for (int i = 0; i < 5; i++) {
			MapMessage message = session.createMapMessage();
			message.setString("Item", "Computer(s)");
			int quantity = (int)(Math.random() * 4) + 1;
			message.setInt("Quantity", quantity);
			message.setJMSReplyTo(retailerConfirmQueue);
			producer.send(message);
			System.out.println("Retailer: Ordered " + quantity + " computers.");
			
			MapMessage reply = (MapMessage) replyConsumer.receive();
			if (reply.getBoolean("OrderAccepted")) {
				System.out.println("Retailer: Order Filled");
			} else {
				System.out.println("Retailer: Order Not Filled");
			}
		}
		
		// Send a non-MapMessage to signal the end
		producer.send(session.createMessage());
		
		replyConsumer.close();
		connection.close();
		
	} catch (JMSException e) {
		e.printStackTrace();
	}
}
 
Example 18
Source File: BodyIsAssignableFromTest.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
/**
 * @param type
 * @throws JMSException
 */
private Object createBodySendAndReceive(JmsMessageType type) throws JMSException {
   Object res = null;
   Message msg = null;
   switch (type) {
      case BYTE:
         BytesMessage mByte = queueProducerSession.createBytesMessage();
         final int size = 20;
         byte[] resByte = new byte[size];
         for (int i = 0; i < size; i++) {
            resByte[i] = (byte) i;
            mByte.writeByte((byte) i);
         }
         msg = mByte;
         res = resByte;
         break;
      case TEXT:
         res = "JMS2";
         msg = queueProducerSession.createTextMessage("JMS2");
         break;
      case STREAM:
         msg = queueProducerSession.createStreamMessage();
         break;
      case OBJECT:
         res = new Double(37.6);
         msg = queueProducerSession.createObjectMessage(new Double(37.6));
         break;
      case MAP:
         MapMessage msg1 = queueProducerSession.createMapMessage();
         msg1.setInt("int", 13);
         msg1.setLong("long", 37L);
         msg1.setString("string", "crocodile");
         msg = msg1;
         Map<String, Object> map = new HashMap<>();
         map.put("int", Integer.valueOf(13));
         map.put("long", Long.valueOf(37L));
         map.put("string", "crocodile");
         res = map;
         break;
      default:
         Assert.fail("no default...");
   }
   Assert.assertNotNull(msg);
   msg.setStringProperty("type", type.toString());
   queueProducer.send(msg);
   return res;
}
 
Example 19
Source File: MapMessageIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 20000)
public void testSendBasicMapMessage() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        Connection connection = testFixture.establishConnecton(testPeer);
        testPeer.expectBegin();
        testPeer.expectSenderAttach();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue("myQueue");
        MessageProducer producer = session.createProducer(queue);

        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;

        // Prepare a MapMessage to send to the test peer to send
        MapMessage mapMessage = session.createMapMessage();

        mapMessage.setBoolean(myBoolKey, myBool);
        mapMessage.setByte(myByteKey, myByte);
        mapMessage.setBytes(myBytesKey, myBytes);
        mapMessage.setChar(myCharKey, myChar);
        mapMessage.setDouble(myDoubleKey, myDouble);
        mapMessage.setFloat(myFloatKey, myFloat);
        mapMessage.setInt(myIntKey, myInt);
        mapMessage.setLong(myLongKey, myLong);
        mapMessage.setShort(myShortKey, myShort);
        mapMessage.setString(myStringKey, myString);

        // prepare a matcher for the test peer to use to receive and verify the message
        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.
        // TODO: see note above to explain the ugly cast
        map.put(myCharKey, (int) 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);

        MessageHeaderSectionMatcher headersMatcher = new MessageHeaderSectionMatcher(true).withDurable(equalTo(true));
        MessageAnnotationsSectionMatcher msgAnnotationsMatcher = new MessageAnnotationsSectionMatcher(true);
        msgAnnotationsMatcher.withEntry(AmqpMessageSupport.JMS_MSG_TYPE, equalTo(AmqpMessageSupport.JMS_MAP_MESSAGE));
        MessagePropertiesSectionMatcher propertiesMatcher = new MessagePropertiesSectionMatcher(true);
        TransferPayloadCompositeMatcher messageMatcher = new TransferPayloadCompositeMatcher();
        messageMatcher.setHeadersMatcher(headersMatcher);
        messageMatcher.setMessageAnnotationsMatcher(msgAnnotationsMatcher);
        messageMatcher.setPropertiesMatcher(propertiesMatcher);
        messageMatcher.setMessageContentMatcher(new EncodedAmqpValueMatcher(map));

        testPeer.expectTransfer(messageMatcher);
        testPeer.expectClose();

        // send the message
        producer.send(mapMessage);

        assertTrue(mapMessage.isBodyAssignableTo(Map.class));
        assertTrue(mapMessage.isBodyAssignableTo(Object.class));
        assertFalse(mapMessage.isBodyAssignableTo(Boolean.class));
        assertFalse(mapMessage.isBodyAssignableTo(byte[].class));

        assertNotNull(mapMessage.getBody(Object.class));
        assertNotNull(mapMessage.getBody(Map.class));
        try {
            mapMessage.getBody(byte[].class);
            fail("Cannot read TextMessage with this type.");
        } catch (MessageFormatException mfe) {
        }

        connection.close();

        testPeer.waitForAllHandlersToComplete(3000);
    }
}
 
Example 20
Source File: MapMessageIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 20000)
public void testSendMapMessageIsWritable() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        Connection connection = testFixture.establishConnecton(testPeer);
        testPeer.expectBegin();
        testPeer.expectSenderAttach();

        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue("myQueue");
        MessageProducer producer = session.createProducer(queue);

        String myIntKey = "myInt";
        int myInt = Integer.MAX_VALUE;
        String myStringKey = "myString";
        String myString = myStringKey;

        // Prepare a MapMessage to send to the test peer to send
        MapMessage mapMessage = session.createMapMessage();

        mapMessage.setString(myStringKey, myString);

        // prepare a matcher for the test peer to use to receive and verify the message
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        map.put(myStringKey, myString);

        MessageHeaderSectionMatcher headersMatcher = new MessageHeaderSectionMatcher(true).withDurable(equalTo(true));
        MessageAnnotationsSectionMatcher msgAnnotationsMatcher = new MessageAnnotationsSectionMatcher(true);
        msgAnnotationsMatcher.withEntry(AmqpMessageSupport.JMS_MSG_TYPE, equalTo(AmqpMessageSupport.JMS_MAP_MESSAGE));
        MessagePropertiesSectionMatcher propertiesMatcher = new MessagePropertiesSectionMatcher(true);
        TransferPayloadCompositeMatcher messageMatcher = new TransferPayloadCompositeMatcher();
        messageMatcher.setHeadersMatcher(headersMatcher);
        messageMatcher.setMessageAnnotationsMatcher(msgAnnotationsMatcher);
        messageMatcher.setPropertiesMatcher(propertiesMatcher);
        messageMatcher.setMessageContentMatcher(new EncodedAmqpValueMatcher(map));

        testPeer.expectTransfer(messageMatcher);

        // send the message
        producer.send(mapMessage);

        // Update the message and matcher and send again
        mapMessage.setInt(myIntKey, myInt);
        map.put(myIntKey, myInt);
        testPeer.expectTransfer(messageMatcher);
        testPeer.expectClose();

        producer.send(mapMessage);

        connection.close();

        testPeer.waitForAllHandlersToComplete(3000);
    }
}