Java Code Examples for javax.jms.Message#getPropertyNames()

The following examples show how to use javax.jms.Message#getPropertyNames() . 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: JmsMessageTransformation.java    From qpid-jms with Apache License 2.0 6 votes vote down vote up
/**
 * Copies the standard JMS and user defined properties from the given source
 * message to the specified target message.  The copy can only handle the JMS
 * specific message properties and known JMS Headers, any headers that are
 * specific to the foreign message may be lost if not returned directly via
 * the <code>propertyNames</code> method.
 *
 * @param connection
 *        The Connection instance that is requesting the transformation.
 * @param source
 *        the message to take the properties from
 * @param target
 *        the message to add the properties to
 *
 * @throws JMSException if an error occurs during the copy of message properties.
 */
public static void copyProperties(JmsConnection connection, Message source, JmsMessage target) throws JMSException {
    target.setJMSMessageID(source.getJMSMessageID());
    target.setJMSCorrelationID(source.getJMSCorrelationID());
    target.setJMSReplyTo(transformDestination(connection, source.getJMSReplyTo()));
    target.setJMSDestination(transformDestination(connection, source.getJMSDestination()));
    target.setJMSDeliveryMode(source.getJMSDeliveryMode());
    target.setJMSDeliveryTime(getForeignMessageDeliveryTime(source));
    target.setJMSRedelivered(source.getJMSRedelivered());
    target.setJMSType(source.getJMSType());
    target.setJMSExpiration(source.getJMSExpiration());
    target.setJMSPriority(source.getJMSPriority());
    target.setJMSTimestamp(source.getJMSTimestamp());

    Enumeration<?> propertyNames = source.getPropertyNames();

    while (propertyNames.hasMoreElements()) {
        String name = propertyNames.nextElement().toString();
        Object obj = source.getObjectProperty(name);
        target.setObjectProperty(name, obj);
    }
}
 
Example 2
Source File: MessagePropertyTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
/**
 * Test that the <code>Message.getPropertyNames()</code> method does not return
 * the name of the JMS standard header fields (e.g. <code>JMSCorrelationID</code>).
 */
@Test
public void testGetPropertyNames() {
   try {
      Message message = senderSession.createMessage();
      message.setJMSCorrelationID("foo");
      Enumeration enumeration = message.getPropertyNames();
      while (enumeration.hasMoreElements()) {
         String propName = (String) enumeration.nextElement();
         boolean valid = !propName.startsWith("JMS") || propName.startsWith("JMSX");
         Assert.assertTrue("sec. 3.5.6 The getPropertyNames method does not return the names of " + "the JMS standard header field [e.g. JMSCorrelationID]: " +
                              propName, valid);
      }
   } catch (JMSException e) {
      fail(e);
   }
}
 
Example 3
Source File: FatalJmsExceptionMessageCreator.java    From jadira with Apache License 2.0 6 votes vote down vote up
protected Map<String, Object> buildErrorMessageProperties(Message msg) throws JMSException {

        Map<String, Object> properties = new HashMap<String, Object>();

        @SuppressWarnings("unchecked")
        Enumeration<String> srcProperties = msg.getPropertyNames();

        while (srcProperties.hasMoreElements()) {
            String propertyName = srcProperties.nextElement();
            properties.put("original_" + propertyName, msg.getObjectProperty(propertyName));
        }

        properties.put(ORIGINAL_DELIVERY_MODE, msg.getJMSDeliveryMode());
        properties.put(ORIGINAL_EXPIRATION, msg.getJMSExpiration());
        properties.put(ORIGINAL_MESSAGE_ID, msg.getJMSMessageID());
        properties.put(ORIGINAL_REPLY_TO, msg.getJMSReplyTo());
        properties.put(ORIGINAL_REDELIVERED, msg.getJMSRedelivered());
        properties.put(ORIGINAL_CORRELATIONID, msg.getJMSCorrelationID());
        properties.put(ORIGINAL_PRIORITY, msg.getJMSPriority());

        return properties;
    }
 
Example 4
Source File: TopicClusterTest.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private void checkInternalProperty(Message... msgs) throws Exception {
   boolean checked = false;
   for (Message m : msgs) {
      ActiveMQMessage hqMessage = (ActiveMQMessage) m;
      ClientMessage coreMessage = hqMessage.getCoreMessage();
      Set<SimpleString> coreProps = coreMessage.getPropertyNames();
      boolean exist = false;
      for (SimpleString prop : coreProps) {
         if (prop.startsWith(org.apache.activemq.artemis.api.core.Message.HDR_ROUTE_TO_IDS)) {
            exist = true;
            break;
         }
      }

      if (exist) {
         Enumeration enumProps = m.getPropertyNames();
         while (enumProps.hasMoreElements()) {
            String propName = (String) enumProps.nextElement();
            assertFalse("Shouldn't be in jms property: " + propName, propName.startsWith(org.apache.activemq.artemis.api.core.Message.HDR_ROUTE_TO_IDS.toString()));
         }
         checked = true;
      }
   }
   assertTrue(checked);
}
 
Example 5
Source File: RequestReplyToTopicViaThreeNetworkHopsTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
public static String fmtMsgInfo(Message msg) throws Exception {
   StringBuilder msg_desc;
   String prop;
   Enumeration<?> prop_enum;

   msg_desc = new StringBuilder();
   msg_desc = new StringBuilder();

   if (msg instanceof TextMessage) {
      msg_desc.append(((TextMessage) msg).getText());
   } else {
      msg_desc.append("[");
      msg_desc.append(msg.getClass().getName());
      msg_desc.append("]");
   }

   prop_enum = msg.getPropertyNames();
   while (prop_enum.hasMoreElements()) {
      prop = (String) prop_enum.nextElement();
      msg_desc.append("; ");
      msg_desc.append(prop);
      msg_desc.append("=");
      msg_desc.append(msg.getStringProperty(prop));
   }

   return msg_desc.toString();
}
 
Example 6
Source File: JMSConsumer.java    From nifi with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Map<String, String> extractMessageProperties(final Message message) {
    final Map<String, String> properties = new HashMap<>();
    try {
        final Enumeration<String> propertyNames = message.getPropertyNames();
        while (propertyNames.hasMoreElements()) {
            String propertyName = propertyNames.nextElement();
            properties.put(propertyName, String.valueOf(message.getObjectProperty(propertyName)));
        }
    } catch (JMSException e) {
        this.processLog.warn("Failed to extract message properties", e);
    }
    return properties;
}
 
Example 7
Source File: A.java    From a with Apache License 2.0 5 votes vote down vote up
protected void outputProperties(Message msg) throws JMSException {
	output("Message Properties");
	@SuppressWarnings("unchecked")
	Enumeration<String> en = msg.getPropertyNames();
	while (en.hasMoreElements()) {
		String name = en.nextElement();
		try {
			Object property = msg.getObjectProperty(name);
			output("  ", name, ": ", null != property ? property.toString() : "[null]");
		} catch ( Exception e) {
			output("  ", name, ": Error loading property (" + e.getMessage() + ")");
		}
	}
}
 
Example 8
Source File: MessagePropertyTest.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
/**
 * Test that the <code>Message.getPropertyNames()</code> methods.
 */
@Test
public void testPropertyIteration() {
   try {
      Message message = senderSession.createMessage();
      Enumeration enumeration = message.getPropertyNames();
      // there can be some properties already defined (e.g. JMSXDeliveryCount)
      int originalCount = 0;
      while (enumeration.hasMoreElements()) {
         enumeration.nextElement();
         originalCount++;
      }
      message.setDoubleProperty("pi", 3.14159);
      enumeration = message.getPropertyNames();
      boolean foundPiProperty = false;
      int newCount = 0;
      while (enumeration.hasMoreElements()) {
         String propName = (String) enumeration.nextElement();
         newCount++;
         if ("pi".equals(propName)) {
            foundPiProperty = true;
         }
      }
      Assert.assertEquals(originalCount + 1, newCount);
      Assert.assertTrue(foundPiProperty);
   } catch (JMSException e) {
      fail(e);
   }
}
 
Example 9
Source File: NotifyMessageListener.java    From DWSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
	 * MessageListener回调函数.
	 */
	@Override
	public void onMessage(Message message) {
		try {
			message.getPropertyNames();
			/*MapMessage mapMessage = (MapMessage) message;
			// 打印消息详情
			String subject=mapMessage.getObject("subject").toString();
			String to=mapMessage.getObject("to").toString();
			String date=mapMessage.getObject("date").toString();
			String username=mapMessage.getObject("username").toString();
			String content=mapMessage.getObject("content").toString();*/
			
			String subject= "柯远";
			String to="xx";
			String date="2013-03-20";
			String username="keyuan";
			String content="activmq测试邮件";
			//mapMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, 15*1000);
			System.out.println("收到了消息");
			logger.info("subject:" + subject+";to:"+to+";date:"+date+";username:"+username+";content:"+content+";");
			Email email=new Email();
			email.setContent(content);
			email.setDate(date);
			email.setSubject(subject);
			email.setTo(to);
			email.setUsername(username);
			// 发送邮件
//			if (simpleMailService != null) {
//				simpleMailService.sendNotificationMail("柯远。。。");
//			}
		} catch (JMSException e) {
			logger.error(e.getMessage(), e);
		}
	}
 
Example 10
Source File: JMSConsumer.java    From oneops with Apache License 2.0 5 votes vote down vote up
private void addData(Message message, String text) throws JMSException {
	MessageData data = new MessageData();
	data.setPayload(text);
	Map<String, String> headers = new HashMap<String, String>();
	Enumeration<String> names = message.getPropertyNames();
	while (names.hasMoreElements()) {
		String name = names.nextElement();
		String value = message.getStringProperty(name);
		headers.put(name, value);
	}
	data.setHeaders(headers);
	messages.add(data);
}
 
Example 11
Source File: JMSConsumer.java    From solace-integration-guides with Apache License 2.0 5 votes vote down vote up
/**
 *
 */
@SuppressWarnings("unchecked")
private Map<String, String> extractMessageProperties(Message message) {
    Map<String, String> properties = new HashMap<>();
    try {
        Enumeration<String> propertyNames = message.getPropertyNames();
        while (propertyNames.hasMoreElements()) {
            String propertyName = propertyNames.nextElement();
            properties.put(propertyName, String.valueOf(message.getObjectProperty(propertyName)));
        }
    } catch (JMSException e) {
        this.processLog.warn("Failed to extract message properties", e);
    }
    return properties;
}
 
Example 12
Source File: JMSConsumer.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 *
 */
@SuppressWarnings("unchecked")
private Map<String, String> extractMessageProperties(Message message) {
    Map<String, String> properties = new HashMap<>();
    try {
        Enumeration<String> propertyNames = message.getPropertyNames();
        while (propertyNames.hasMoreElements()) {
            String propertyName = propertyNames.nextElement();
            properties.put(propertyName, String.valueOf(message.getObjectProperty(propertyName)));
        }
    } catch (JMSException e) {
        this.processLog.warn("Failed to extract message properties", e);
    }
    return properties;
}
 
Example 13
Source File: ITJms.java    From brave with Apache License 2.0 5 votes vote down vote up
static Map<String, String> propertiesToMap(Message headers) {
  try {
    Map<String, String> result = new LinkedHashMap<>();
    Enumeration<String> names = headers.getPropertyNames();
    while (names.hasMoreElements()) {
      String name = names.nextElement();
      result.put(name, headers.getStringProperty(name));
    }
    return result;
  } catch (JMSException e) {
    throw new AssertionError(e);
  }
}
 
Example 14
Source File: JmsMessageHeadersExtractor.java    From AuTe-Framework with Apache License 2.0 5 votes vote down vote up
private Map<Object, Object> getMessageHeaders(Message message) throws JMSException {
    Map<Object, Object> headers = new HashMap<>();
    Enumeration names = message.getPropertyNames();
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        headers.put(name, message.getObjectProperty(name));
    }
    return headers;
}
 
Example 15
Source File: JMSUtils.java    From micro-integrator with Apache License 2.0 4 votes vote down vote up
/**
 * Extract transport level headers from JMS message into a Map
 *
 * @param message    JMS message
 * @param msgContext axis2 message context
 * @return a Map of the transport headers
 */
public static Map<String, Object> getTransportHeaders(Message message, MessageContext msgContext) {
    // create a Map to hold transport headers
    Map<String, Object> map = new HashMap<>();

    try {
        Enumeration<?> propertyNamesEnm = message.getPropertyNames();

        while (propertyNamesEnm.hasMoreElements()) {
            String headerName = (String) propertyNamesEnm.nextElement();
            Object headerValue = message.getObjectProperty(headerName);

            if (headerValue instanceof String) {
                if (isHyphenReplaceMode(msgContext)) {
                    map.put(inverseTransformHyphenatedString(headerName), message.getStringProperty(headerName));
                } else {
                    map.put(headerName, message.getStringProperty(headerName));
                }
            } else if (headerValue instanceof Integer) {
                map.put(headerName, message.getIntProperty(headerName));
            } else if (headerValue instanceof Boolean) {
                map.put(headerName, message.getBooleanProperty(headerName));
            } else if (headerValue instanceof Long) {
                map.put(headerName, message.getLongProperty(headerName));
            } else if (headerValue instanceof Double) {
                map.put(headerName, message.getDoubleProperty(headerName));
            } else if (headerValue instanceof Float) {
                map.put(headerName, message.getFloatProperty(headerName));
            } else {
                map.put(headerName, headerValue);
            }
        }

    } catch (JMSException e) {
        log.error("Error while reading the Transport Headers from JMS Message", e);
    }

    // remove "INTERNAL_TRANSACTION_COUNTED" header from the transport level headers map.
    // this property will be maintained in the message context. Therefore, no need to set this in the transport
    // headers.
    map.remove(BaseConstants.INTERNAL_TRANSACTION_COUNTED);
    return map;
}
 
Example 16
Source File: JmsFactory.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
public static Map<String, String> createAttributeMap(final Message message) throws JMSException {
    final Map<String, String> attributes = new HashMap<>();

    final Enumeration<?> enumeration = message.getPropertyNames();
    while (enumeration.hasMoreElements()) {
        final String propName = (String) enumeration.nextElement();

        final Object value = message.getObjectProperty(propName);

        if (value == null) {
            attributes.put(ATTRIBUTE_PREFIX + propName, "");
            attributes.put(ATTRIBUTE_PREFIX + propName + ATTRIBUTE_TYPE_SUFFIX, "Unknown");
            continue;
        }

        final String valueString = value.toString();
        attributes.put(ATTRIBUTE_PREFIX + propName, valueString);

        final String propType;
        if (value instanceof String) {
            propType = PROP_TYPE_STRING;
        } else if (value instanceof Double) {
            propType = PROP_TYPE_DOUBLE;
        } else if (value instanceof Float) {
            propType = PROP_TYPE_FLOAT;
        } else if (value instanceof Long) {
            propType = PROP_TYPE_LONG;
        } else if (value instanceof Integer) {
            propType = PROP_TYPE_INTEGER;
        } else if (value instanceof Short) {
            propType = PROP_TYPE_SHORT;
        } else if (value instanceof Byte) {
            propType = PROP_TYPE_BYTE;
        } else if (value instanceof Boolean) {
            propType = PROP_TYPE_BOOLEAN;
        } else {
            propType = PROP_TYPE_OBJECT;
        }

        attributes.put(ATTRIBUTE_PREFIX + propName + ATTRIBUTE_TYPE_SUFFIX, propType);
    }

    if (message.getJMSCorrelationID() != null) {
        attributes.put(ATTRIBUTE_PREFIX + JMS_CORRELATION_ID, message.getJMSCorrelationID());
    }
    if (message.getJMSDestination() != null) {
        String destinationName;
        if (message.getJMSDestination() instanceof Queue) {
            destinationName = ((Queue) message.getJMSDestination()).getQueueName();
        } else {
            destinationName = ((Topic) message.getJMSDestination()).getTopicName();
        }
        attributes.put(ATTRIBUTE_PREFIX + JMS_DESTINATION, destinationName);
    }
    if (message.getJMSMessageID() != null) {
        attributes.put(ATTRIBUTE_PREFIX + JMS_MESSAGE_ID, message.getJMSMessageID());
    }
    if (message.getJMSReplyTo() != null) {
        attributes.put(ATTRIBUTE_PREFIX + JMS_REPLY_TO, message.getJMSReplyTo().toString());
    }
    if (message.getJMSType() != null) {
        attributes.put(ATTRIBUTE_PREFIX + JMS_TYPE, message.getJMSType());
    }

    attributes.put(ATTRIBUTE_PREFIX + JMS_DELIVERY_MODE, String.valueOf(message.getJMSDeliveryMode()));
    attributes.put(ATTRIBUTE_PREFIX + JMS_EXPIRATION, String.valueOf(message.getJMSExpiration()));
    attributes.put(ATTRIBUTE_PREFIX + JMS_PRIORITY, String.valueOf(message.getJMSPriority()));
    attributes.put(ATTRIBUTE_PREFIX + JMS_REDELIVERED, String.valueOf(message.getJMSRedelivered()));
    attributes.put(ATTRIBUTE_PREFIX + JMS_TIMESTAMP, String.valueOf(message.getJMSTimestamp()));
    return attributes;
}
 
Example 17
Source File: MessageDumpWriter.java    From a with Apache License 2.0 4 votes vote down vote up
public MessageDump toDumpMessage(Message msg) throws JMSException{
	
	MessageDump dump = new MessageDump();
	dump.JMSCorrelationID = msg.getJMSCorrelationID();
	dump.JMSMessageID = msg.getJMSMessageID();
	dump.JMSType = msg.getJMSType();
	dump.JMSDeliveryMode =  msg.getJMSDeliveryMode();
	dump.JMSExpiration = msg.getJMSExpiration();
	dump.JMSRedelivered = msg.getJMSRedelivered();
	dump.JMSTimestamp =  msg.getJMSTimestamp();
	dump.JMSPriority = msg.getJMSPriority();
	
	@SuppressWarnings("rawtypes")
	Enumeration propertyNames = msg.getPropertyNames();
	while(propertyNames.hasMoreElements()){
		String property = (String) propertyNames.nextElement();
		Object propertyValue = msg.getObjectProperty(property);
		if( propertyValue instanceof String){
			dump.stringProperties.put(property, (String)propertyValue);
		} else if ( propertyValue instanceof Integer ){
			dump.intProperties.put(property, (Integer)propertyValue);
		} else if ( propertyValue instanceof Long) {
			dump.longProperties.put(property, (Long)propertyValue);
		} else if( propertyValue instanceof Double) {
			dump.doubleProperties.put(property, (Double) propertyValue);
		} else if (propertyValue instanceof Short) {
			dump.shortProperties.put(property, (Short)propertyValue);
		} else if (propertyValue instanceof Float) {
			dump.floatProperties.put(property, (Float) propertyValue);
		} else if (propertyValue instanceof Byte) {
			dump.byteProperties.put(property, (Byte)propertyValue);
		} else if (propertyValue instanceof Boolean) {
			dump.boolProperties.put(property, (Boolean)propertyValue);
		} else if (propertyValue instanceof Serializable){
			// Object property.. if it's on Classpath and Serializable
			byte[] propBytes = SerializationUtils.serialize((Serializable) propertyValue);
			dump.objectProperties.put(property, Base64.encodeBase64String(propBytes));
		} else {
			// Corner case.
			throw new IllegalArgumentException("Property of key '"+ property +"' is not serializable. Type is: " + propertyValue.getClass().getCanonicalName());
		}
	}
	
	dump.body = "";
	dump.type = "";
	
	if (msg instanceof TextMessage) {
		dump.body = ((TextMessage)msg).getText();
		dump.type = "TextMessage";
	} else if (msg instanceof BytesMessage) {
		BytesMessage bm = (BytesMessage)msg;
		byte[] bytes = new byte[(int) bm.getBodyLength()];
		bm.readBytes(bytes);
		dump.body = Base64.encodeBase64String(bytes);
		dump.type = "BytesMessage";
	} else if (msg instanceof ObjectMessage) {
		ObjectMessage om = (ObjectMessage)msg;
		byte[] objectBytes = SerializationUtils.serialize(om.getObject());
		dump.body = Base64.encodeBase64String(objectBytes);
		dump.type = "ObjectMessage";
	}
	return dump;
}
 
Example 18
Source File: MessageIntegrationTest.java    From qpid-jms with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that when receiving a message with the group-id, reply-to-group-id, and group-sequence
 * fields of the AMQP properties section set, that the expected JMSX or JMS_AMQP properties
 * are present, and the expected values are returned when retrieved from the JMS message.
 *
 * @throws Exception if an error occurs during the test.
 */
@Test(timeout = 20000)
public void testReceivedMessageWithGroupRelatedPropertiesSet() throws Exception {
    try (TestAmqpPeer testPeer = new TestAmqpPeer();) {
        Connection connection = testFixture.establishConnecton(testPeer);
        connection.start();

        testPeer.expectBegin();

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

        PropertiesDescribedType props = new PropertiesDescribedType();
        DescribedType amqpValueNullContent = new AmqpValueDescribedType(null);
        MessageAnnotationsDescribedType ann = null;

        String expectedGroupId = "myGroupId123";
        int expectedGroupSeq = 1;
        String expectedReplyToGroupId = "myReplyToGroupId456";

        props.setGroupId(expectedGroupId);
        props.setGroupSequence(UnsignedInteger.valueOf(expectedGroupSeq));
        props.setReplyToGroupId(expectedReplyToGroupId);
        props.setMessageId("myMessageIDString");

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

        MessageConsumer messageConsumer = session.createConsumer(queue);
        Message receivedMessage = messageConsumer.receive(3000);
        testPeer.waitForAllHandlersToComplete(3000);

        assertNotNull("did not receive the message", receivedMessage);

        boolean foundGroupId = false;
        boolean foundGroupSeq = false;
        boolean foundReplyToGroupId = false;

        Enumeration<?> names = receivedMessage.getPropertyNames();
        assertTrue("Message had no property names", names.hasMoreElements());
        while (names.hasMoreElements()) {
            Object element = names.nextElement();

            if (JmsClientProperties.JMSXGROUPID.equals(element)) {
                foundGroupId = true;
            }

            if (JmsClientProperties.JMSXGROUPSEQ.equals(element)) {
                foundGroupSeq = true;
            }

            if (AmqpMessageSupport.JMS_AMQP_REPLY_TO_GROUP_ID.equals(element)) {
                foundReplyToGroupId = true;
            }
        }

        assertTrue("JMSXGroupID not in property names", foundGroupId);
        assertTrue("JMSXGroupSeq  not in property names", foundGroupSeq);
        assertTrue("JMS_AMQP_REPLY_TO_GROUP_ID not in property names", foundReplyToGroupId);

        assertTrue("JMSXGroupID does not exist", receivedMessage.propertyExists(JmsClientProperties.JMSXGROUPID));
        assertTrue("JMSXGroupSeq does not exist", receivedMessage.propertyExists(JmsClientProperties.JMSXGROUPSEQ));
        assertTrue("JMS_AMQP_REPLY_TO_GROUP_ID does not exist", receivedMessage.propertyExists(AmqpMessageSupport.JMS_AMQP_REPLY_TO_GROUP_ID));

        assertEquals("did not get the expected JMSXGroupID", expectedGroupId, receivedMessage.getStringProperty(JmsClientProperties.JMSXGROUPID));
        assertEquals("did not get the expected JMSXGroupSeq", expectedGroupSeq, receivedMessage.getIntProperty(JmsClientProperties.JMSXGROUPSEQ));
        assertEquals("did not get the expected JMS_AMQP_REPLY_TO_GROUP_ID", expectedReplyToGroupId, receivedMessage.getStringProperty(AmqpMessageSupport.JMS_AMQP_REPLY_TO_GROUP_ID));

        testPeer.expectClose();
        connection.close();

        testPeer.waitForAllHandlersToComplete(1000);
    }
}
 
Example 19
Source File: SQSMessageTest.java    From amazon-sqs-java-messaging-lib with Apache License 2.0 4 votes vote down vote up
/**
 * Test setting SQSMessage property
 */
@Test
public void testProperty() throws JMSException {
    when(mockSQSSession.createMessage()).thenReturn(new SQSMessage());
    Message message = mockSQSSession.createMessage();

    message.setBooleanProperty("myTrueBoolean", true);
    message.setBooleanProperty("myFalseBoolean", false);
    message.setIntProperty("myInteger", 100);
    message.setDoubleProperty("myDouble", 2.1768);
    message.setFloatProperty("myFloat", 3.1457f);
    message.setLongProperty("myLong", 1290772974281L);
    message.setShortProperty("myShort", (short) 123);
    message.setByteProperty("myByteProperty", (byte) 'a');
    message.setStringProperty("myString", "StringValue");
    message.setStringProperty("myNumber", "500");

    Assert.assertTrue(message.propertyExists("myTrueBoolean"));
    Assert.assertEquals(message.getObjectProperty("myTrueBoolean"), true);
    Assert.assertEquals(message.getBooleanProperty("myTrueBoolean"), true);
    
    Assert.assertTrue(message.propertyExists("myFalseBoolean"));
    Assert.assertEquals(message.getObjectProperty("myFalseBoolean"), false);
    Assert.assertEquals(message.getBooleanProperty("myFalseBoolean"), false);
    
    Assert.assertTrue(message.propertyExists("myInteger"));
    Assert.assertEquals(message.getObjectProperty("myInteger"), 100);
    Assert.assertEquals(message.getIntProperty("myInteger"), 100);
    
    Assert.assertTrue(message.propertyExists("myDouble"));
    Assert.assertEquals(message.getObjectProperty("myDouble"), 2.1768);
    Assert.assertEquals(message.getDoubleProperty("myDouble"), 2.1768);
    
    Assert.assertTrue(message.propertyExists("myFloat"));
    Assert.assertEquals(message.getObjectProperty("myFloat"), 3.1457f);
    Assert.assertEquals(message.getFloatProperty("myFloat"), 3.1457f);
    
    Assert.assertTrue(message.propertyExists("myLong"));
    Assert.assertEquals(message.getObjectProperty("myLong"), 1290772974281L);
    Assert.assertEquals(message.getLongProperty("myLong"), 1290772974281L);
    
    Assert.assertTrue(message.propertyExists("myShort"));
    Assert.assertEquals(message.getObjectProperty("myShort"), (short) 123);
    Assert.assertEquals(message.getShortProperty("myShort"), (short) 123);
    
    Assert.assertTrue(message.propertyExists("myByteProperty"));
    Assert.assertEquals(message.getObjectProperty("myByteProperty"), (byte) 'a');
    Assert.assertEquals(message.getByteProperty("myByteProperty"), (byte) 'a');
    
    Assert.assertTrue(message.propertyExists("myString"));
    Assert.assertEquals(message.getObjectProperty("myString"), "StringValue");
    Assert.assertEquals(message.getStringProperty("myString"), "StringValue");

    Assert.assertTrue(message.propertyExists("myNumber"));
    Assert.assertEquals(message.getObjectProperty("myNumber"), "500");
    Assert.assertEquals(message.getStringProperty("myNumber"), "500");
    Assert.assertEquals(message.getLongProperty("myNumber"), 500L);
    Assert.assertEquals(message.getFloatProperty("myNumber"), 500f);
    Assert.assertEquals(message.getShortProperty("myNumber"), (short) 500);
    Assert.assertEquals(message.getDoubleProperty("myNumber"), 500d);
    Assert.assertEquals(message.getIntProperty("myNumber"), 500);

    // Validate property names
    Set<String> propertyNamesSet = new HashSet<String>(Arrays.asList(
            "myTrueBoolean",
            "myFalseBoolean",
            "myInteger",
            "myDouble",
            "myFloat",
            "myLong",
            "myShort",
            "myByteProperty",
            "myNumber",
            "myString"));

    Enumeration<String > propertyNames = message.getPropertyNames();
    int counter = 0;
    while (propertyNames.hasMoreElements()) {
        assertTrue(propertyNamesSet.contains(propertyNames.nextElement()));
        counter++;
    }
    assertEquals(propertyNamesSet.size(), counter);
    
    message.clearProperties();
    Assert.assertFalse(message.propertyExists("myTrueBoolean"));
    Assert.assertFalse(message.propertyExists("myInteger"));
    Assert.assertFalse(message.propertyExists("myDouble"));
    Assert.assertFalse(message.propertyExists("myFloat"));
    Assert.assertFalse(message.propertyExists("myLong"));
    Assert.assertFalse(message.propertyExists("myShort"));
    Assert.assertFalse(message.propertyExists("myByteProperty"));
    Assert.assertFalse(message.propertyExists("myString"));
    Assert.assertFalse(message.propertyExists("myNumber"));

    propertyNames = message.getPropertyNames();
    assertFalse(propertyNames.hasMoreElements());
}
 
Example 20
Source File: JmsFactory.java    From nifi with Apache License 2.0 4 votes vote down vote up
public static Map<String, String> createAttributeMap(final Message message) throws JMSException {
    final Map<String, String> attributes = new HashMap<>();

    final Enumeration<?> enumeration = message.getPropertyNames();
    while (enumeration.hasMoreElements()) {
        final String propName = (String) enumeration.nextElement();

        final Object value = message.getObjectProperty(propName);

        if (value == null) {
            attributes.put(ATTRIBUTE_PREFIX + propName, "");
            attributes.put(ATTRIBUTE_PREFIX + propName + ATTRIBUTE_TYPE_SUFFIX, "Unknown");
            continue;
        }

        final String valueString = value.toString();
        attributes.put(ATTRIBUTE_PREFIX + propName, valueString);

        final String propType;
        if (value instanceof String) {
            propType = PROP_TYPE_STRING;
        } else if (value instanceof Double) {
            propType = PROP_TYPE_DOUBLE;
        } else if (value instanceof Float) {
            propType = PROP_TYPE_FLOAT;
        } else if (value instanceof Long) {
            propType = PROP_TYPE_LONG;
        } else if (value instanceof Integer) {
            propType = PROP_TYPE_INTEGER;
        } else if (value instanceof Short) {
            propType = PROP_TYPE_SHORT;
        } else if (value instanceof Byte) {
            propType = PROP_TYPE_BYTE;
        } else if (value instanceof Boolean) {
            propType = PROP_TYPE_BOOLEAN;
        } else {
            propType = PROP_TYPE_OBJECT;
        }

        attributes.put(ATTRIBUTE_PREFIX + propName + ATTRIBUTE_TYPE_SUFFIX, propType);
    }

    if (message.getJMSCorrelationID() != null) {
        attributes.put(ATTRIBUTE_PREFIX + JMS_CORRELATION_ID, message.getJMSCorrelationID());
    }
    if (message.getJMSDestination() != null) {
        String destinationName;
        if (message.getJMSDestination() instanceof Queue) {
            destinationName = ((Queue) message.getJMSDestination()).getQueueName();
        } else {
            destinationName = ((Topic) message.getJMSDestination()).getTopicName();
        }
        attributes.put(ATTRIBUTE_PREFIX + JMS_DESTINATION, destinationName);
    }
    if (message.getJMSMessageID() != null) {
        attributes.put(ATTRIBUTE_PREFIX + JMS_MESSAGE_ID, message.getJMSMessageID());
    }
    if (message.getJMSReplyTo() != null) {
        attributes.put(ATTRIBUTE_PREFIX + JMS_REPLY_TO, message.getJMSReplyTo().toString());
    }
    if (message.getJMSType() != null) {
        attributes.put(ATTRIBUTE_PREFIX + JMS_TYPE, message.getJMSType());
    }

    attributes.put(ATTRIBUTE_PREFIX + JMS_DELIVERY_MODE, String.valueOf(message.getJMSDeliveryMode()));
    attributes.put(ATTRIBUTE_PREFIX + JMS_EXPIRATION, String.valueOf(message.getJMSExpiration()));
    attributes.put(ATTRIBUTE_PREFIX + JMS_PRIORITY, String.valueOf(message.getJMSPriority()));
    attributes.put(ATTRIBUTE_PREFIX + JMS_REDELIVERED, String.valueOf(message.getJMSRedelivered()));
    attributes.put(ATTRIBUTE_PREFIX + JMS_TIMESTAMP, String.valueOf(message.getJMSTimestamp()));
    return attributes;
}