List of usage examples for javax.jms MapMessage setObject
void setObject(String name, Object value) throws JMSException;
From source file:com.mirth.connect.connectors.jms.JmsMessageUtils.java
public static Message getMessageForObject(Object object, Session session) throws JMSException { if (object instanceof Message) { return (Message) object; } else if (object instanceof String) { TextMessage text = session.createTextMessage((String) object); return text; } else if (object instanceof Map) { MapMessage map = session.createMapMessage(); Map.Entry entry = null;//from ww w . j a va 2s . co m Map temp = (Map) object; for (Iterator i = temp.entrySet().iterator(); i.hasNext();) { entry = (Map.Entry) i.next(); map.setObject(entry.getKey().toString(), entry.getValue()); } return map; } else if (object instanceof InputStream) { StreamMessage stream = session.createStreamMessage(); InputStream temp = (InputStream) object; byte[] buffer = new byte[1024 * 4]; int len = 0; try { while ((len = temp.read(buffer)) != -1) { stream.writeBytes(buffer, 0, len); } } catch (IOException e) { throw new JMSException("Failed to read input stream to create a stream message: " + e); } return stream; } else if (object instanceof byte[]) { BytesMessage bytes = session.createBytesMessage(); byte[] buf = (byte[]) object; for (int i = 0; i < buf.length; i++) { bytes.writeByte(buf[i]); } return bytes; } else if (object instanceof Serializable) { ObjectMessage oMsg = session.createObjectMessage(); oMsg.setObject((Serializable) object); return oMsg; } else { throw new JMSException( "Source was not a supported type, data must be Serializable, String, byte[], Map or InputStream"); } }
From source file:org.openmrs.event.EventEngine.java
/** * @param dest// ww w . java2s.c o m * @param eventMessage */ private void doFireEvent(final Destination dest, final EventMessage eventMessage) { initializeIfNeeded(); jmsTemplate.send(dest, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { if (log.isInfoEnabled()) log.info("Sending data " + eventMessage); MapMessage mapMessage = session.createMapMessage(); if (eventMessage != null) { for (Map.Entry<String, Serializable> entry : eventMessage.entrySet()) { mapMessage.setObject(entry.getKey(), entry.getValue()); } } return mapMessage; } }); }
From source file:hermes.impl.DefaultXMLHelper.java
public Message createMessage(MessageFactory hermes, XMLMessage message) throws JMSException, IOException, ClassNotFoundException, DecoderException { try {/*from ww w .j ava 2 s . c om*/ Message rval = hermes.createMessage(); if (message instanceof XMLTextMessage) { rval = hermes.createTextMessage(); XMLTextMessage textMessage = (XMLTextMessage) message; TextMessage textRval = (TextMessage) rval; if (BASE64_CODEC.equals(textMessage.getCodec())) { byte[] bytes = base64EncoderTL.get().decode(textMessage.getText().getBytes()); textRval.setText(new String(bytes, "ASCII")); } else { textRval.setText(textMessage.getText()); } } else if (message instanceof XMLMapMessage) { rval = hermes.createMapMessage(); XMLMapMessage mapMessage = (XMLMapMessage) message; MapMessage mapRval = (MapMessage) rval; for (Iterator iter = mapMessage.getBodyProperty().iterator(); iter.hasNext();) { final Property property = (Property) iter.next(); if (property.getValue() == null) { mapRval.setObject(property.getName(), null); } else if (property.getType().equals(String.class.getName())) { mapRval.setString(property.getName(), property.getValue()); } else if (property.getType().equals(Long.class.getName())) { mapRval.setLong(property.getName(), Long.parseLong(property.getValue())); } else if (property.getType().equals(Double.class.getName())) { mapRval.setDouble(property.getName(), Double.parseDouble(property.getValue())); } else if (property.getType().equals(Boolean.class.getName())) { mapRval.setBoolean(property.getName(), Boolean.getBoolean(property.getValue())); } else if (property.getType().equals(Character.class.getName())) { mapRval.setChar(property.getName(), property.getValue().charAt(0)); } else if (property.getType().equals(Short.class.getName())) { mapRval.setShort(property.getName(), Short.parseShort(property.getValue())); } else if (property.getType().equals(Integer.class.getName())) { mapRval.setInt(property.getName(), Integer.parseInt(property.getValue())); } } } else if (message instanceof XMLBytesMessage) { rval = hermes.createBytesMessage(); XMLBytesMessage bytesMessage = (XMLBytesMessage) message; BytesMessage bytesRval = (BytesMessage) rval; bytesRval.writeBytes(base64EncoderTL.get().decode(bytesMessage.getBytes().getBytes())); } else if (message instanceof XMLObjectMessage) { rval = hermes.createObjectMessage(); XMLObjectMessage objectMessage = (XMLObjectMessage) message; ObjectMessage objectRval = (ObjectMessage) rval; ByteArrayInputStream bistream = new ByteArrayInputStream( base64EncoderTL.get().decode(objectMessage.getObject().getBytes())); ObjectInputStream oistream = new ObjectInputStream(bistream); objectRval.setObject((Serializable) oistream.readObject()); } // // JMS Header properties try { rval.setJMSDeliveryMode(message.getJMSDeliveryMode()); } catch (JMSException ex) { log.error("unable to set JMSDeliveryMode to " + message.getJMSDeliveryMode() + ": " + ex.getMessage()); } try { rval.setJMSMessageID(message.getJMSMessageID()); } catch (JMSException ex) { log.error("unable to set JMSMessageID: " + ex.getMessage(), ex); } try { if (message.getJMSExpiration() != null) { rval.setJMSExpiration(message.getJMSExpiration()); } } catch (JMSException ex) { log.error("unable to set JMSExpiration: " + ex.getMessage(), ex); } try { if (message.getJMSPriority() != null) { rval.setJMSPriority(message.getJMSPriority()); } } catch (JMSException ex) { log.error("unable to set JMSPriority: " + ex.getMessage(), ex); } try { if (message.getJMSTimestamp() != null) { rval.setJMSTimestamp(message.getJMSTimestamp()); } } catch (JMSException ex) { log.error("unable to set JMSTimestamp:" + ex.getMessage(), ex); } if (message.getJMSCorrelationID() != null) { rval.setJMSCorrelationID(message.getJMSCorrelationID()); } if (message.getJMSReplyTo() != null && !message.getJMSReplyTo().equals("null")) { rval.setJMSReplyTo(hermes.getDestination(message.getJMSReplyTo(), Domain.getDomain(message.getJMSReplyToDomain()))); } if (message.getJMSType() != null) { rval.setJMSType(message.getJMSType()); } if (message.getJMSDestination() != null) { if (message.isFromQueue()) { rval.setJMSDestination(hermes.getDestination(message.getJMSDestination(), Domain.QUEUE)); } else { rval.setJMSDestination(hermes.getDestination(message.getJMSDestination(), Domain.TOPIC)); } } for (Iterator iter = message.getHeaderProperty().iterator(); iter.hasNext();) { Property property = (Property) iter.next(); if (property.getValue() == null) { rval.setObjectProperty(property.getName(), null); } else if (property.getType().equals(String.class.getName())) { rval.setStringProperty(property.getName(), StringEscapeUtils.unescapeXml(property.getValue())); } else if (property.getType().equals(Long.class.getName())) { rval.setLongProperty(property.getName(), Long.parseLong(property.getValue())); } else if (property.getType().equals(Double.class.getName())) { rval.setDoubleProperty(property.getName(), Double.parseDouble(property.getValue())); } else if (property.getType().equals(Boolean.class.getName())) { rval.setBooleanProperty(property.getName(), Boolean.parseBoolean(property.getValue())); } else if (property.getType().equals(Short.class.getName())) { rval.setShortProperty(property.getName(), Short.parseShort(property.getValue())); } else if (property.getType().equals(Integer.class.getName())) { rval.setIntProperty(property.getName(), Integer.parseInt(property.getValue())); } } return rval; } catch (NamingException e) { throw new HermesException(e); } }
From source file:com.espertech.esperio.jms.JMSDefaultMapMessageMarshaller.java
public Message marshal(EventBean eventBean, Session session, long timestamp) throws EPException { EventType eventType = eventBean.getEventType(); MapMessage mapMessage = null; try {//from w w w . j a va 2 s. co m mapMessage = session.createMapMessage(); String[] properties = eventType.getPropertyNames(); for (String property : properties) { if ((ExecutionPathDebugLog.isDebugEnabled) && (log.isDebugEnabled())) { log.debug( ".Marshal EventProperty property==" + property + ", value=" + eventBean.get(property)); } Class clazz = eventType.getPropertyType(property); if (JavaClassHelper.isNumeric(clazz)) { Class boxedClazz = JavaClassHelper.getBoxedType(clazz); if (boxedClazz == Double.class) { mapMessage.setDouble(property, (Double) eventBean.get(property)); } if (boxedClazz == Float.class) { mapMessage.setFloat(property, (Float) eventBean.get(property)); } if (boxedClazz == Byte.class) { mapMessage.setFloat(property, (Byte) eventBean.get(property)); } if (boxedClazz == Short.class) { mapMessage.setShort(property, (Short) eventBean.get(property)); } if (boxedClazz == Integer.class) { mapMessage.setInt(property, (Integer) eventBean.get(property)); } if (boxedClazz == Long.class) { mapMessage.setLong(property, (Long) eventBean.get(property)); } } else if ((clazz == boolean.class) || (clazz == Boolean.class)) { mapMessage.setBoolean(property, (Boolean) eventBean.get(property)); } else if ((clazz == Character.class) || (clazz == char.class)) { mapMessage.setChar(property, (Character) eventBean.get(property)); } else if (clazz == String.class) { mapMessage.setString(property, (String) eventBean.get(property)); } else { mapMessage.setObject(property, eventBean.get(property)); } } mapMessage.setJMSTimestamp(timestamp); } catch (JMSException ex) { throw new EPException(ex); } return mapMessage; }
From source file:org.apache.camel.component.jms.JmsBinding.java
/** * Populates a {@link MapMessage} from a {@link Map} instance. *///from www . ja v a 2s. c o m protected void populateMapMessage(MapMessage message, Map<?, ?> map, CamelContext context) throws JMSException { for (Object key : map.keySet()) { String keyString = CamelContextHelper.convertTo(context, String.class, key); if (keyString != null) { message.setObject(keyString, map.get(key)); } } }
From source file:org.gss_project.gss.server.ejb.AdminAPIBean.java
public void indexFile(Long fileId, boolean delete) { Connection qConn = null;/*ww w. j a v a 2 s .c o m*/ Session session = null; MessageProducer sender = null; try { Context jndiCtx = new InitialContext(); ConnectionFactory factory = (QueueConnectionFactory) jndiCtx.lookup("java:/JmsXA"); Queue queue = (Queue) jndiCtx.lookup("queue/gss-indexingQueue"); qConn = factory.createConnection(); session = qConn.createSession(false, Session.AUTO_ACKNOWLEDGE); sender = session.createProducer(queue); MapMessage map = session.createMapMessage(); map.setObject("id", fileId); map.setBoolean("delete", delete); sender.send(map); } catch (NamingException e) { logger.error("Index was not updated: ", e); } catch (JMSException e) { logger.error("Index was not updated: ", e); } finally { try { if (sender != null) sender.close(); if (session != null) session.close(); if (qConn != null) qConn.close(); } catch (JMSException e) { logger.warn(e); } } }
From source file:org.gss_project.gss.server.ejb.ExternalAPIBean.java
private void indexFile(Long fileId, boolean delete) { Connection qConn = null;// w ww . j av a2s . co m Session session = null; MessageProducer sender = null; try { Context jndiCtx = new InitialContext(); ConnectionFactory factory = (QueueConnectionFactory) jndiCtx.lookup("java:/JmsXA"); Queue queue = (Queue) jndiCtx.lookup("queue/gss-indexingQueue"); qConn = factory.createConnection(); session = qConn.createSession(false, Session.AUTO_ACKNOWLEDGE); sender = session.createProducer(queue); MapMessage map = session.createMapMessage(); map.setObject("id", fileId); map.setBoolean("delete", delete); sender.send(map); } catch (NamingException e) { logger.error("Index was not updated: ", e); } catch (JMSException e) { logger.error("Index was not updated: ", e); } finally { try { if (sender != null) sender.close(); if (session != null) session.close(); if (qConn != null) qConn.close(); } catch (JMSException e) { logger.warn(e); } } }
From source file:org.mule.transport.jms.JmsMessageUtils.java
private static Message mapToMessage(Map<?, ?> value, Session session) throws JMSException { MapMessage mMsg = session.createMapMessage(); for (Iterator<?> i = value.entrySet().iterator(); i.hasNext();) { Map.Entry<?, ?> entry = (Map.Entry<?, ?>) i.next(); mMsg.setObject(entry.getKey().toString(), entry.getValue()); }/*from w ww .jav a 2s. co m*/ return mMsg; }
From source file:org.wso2.carbon.event.output.adapter.jms.internal.util.JMSMessageSender.java
public Message convertToJMSMessage(Object messageObj, Map<String, String> messageProperties, Session session) { Message jmsMessage = null;/*from w ww .ja v a2 s . c om*/ try { if (messageObj instanceof OMElement) { jmsMessage = session.createTextMessage(messageObj.toString()); } else if (messageObj instanceof String) { jmsMessage = session.createTextMessage((String) messageObj); } else if (messageObj instanceof Map) { MapMessage mapMessage = session.createMapMessage(); Map sourceMessage = (Map) messageObj; for (Object key : sourceMessage.keySet()) { mapMessage.setObject((String) key, sourceMessage.get(key)); } jmsMessage = mapMessage; } } catch (JMSException e) { handleException("Failed to publish to topic:" + messageProperties.get(JMSConstants.PARAM_DESTINATION), e); } return jmsMessage; }
From source file:org.wso2.carbon.event.output.adaptor.jms.internal.util.JMSMessageSender.java
public Message convertToJMSMessage(Object messageObj, Map<String, String> messageProperties) { Message jmsMessage = null;//from ww w. j a va2s. com try { if (messageObj instanceof OMElement) { jmsMessage = session.createTextMessage(messageObj.toString()); } else if (messageObj instanceof String) { jmsMessage = session.createTextMessage((String) messageObj); } else if (messageObj instanceof Map) { MapMessage mapMessage = session.createMapMessage(); Map sourceMessage = (Map) messageObj; for (Object key : sourceMessage.keySet()) { mapMessage.setObject((String) key, sourceMessage.get(key)); } jmsMessage = mapMessage; } } catch (JMSException e) { handleException("Failed to publish to topic:" + messageProperties.get(JMSConstants.PARAM_DESTINATION), e); } return jmsMessage; }