List of usage examples for javax.jms BytesMessage readBytes
int readBytes(byte[] value) throws JMSException;
From source file:ch.algotrader.event.TopicEventDumper.java
public static void main(String... args) throws Exception { SingleConnectionFactory jmsActiveMQFactory = new SingleConnectionFactory( new ActiveMQConnectionFactory("tcp://localhost:61616")); jmsActiveMQFactory.afterPropertiesSet(); jmsActiveMQFactory.resetConnection(); ActiveMQTopic topic = new ActiveMQTopic(">"); DefaultMessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer(); messageListenerContainer.setDestination(topic); messageListenerContainer.setConnectionFactory(jmsActiveMQFactory); messageListenerContainer.setSubscriptionShared(false); messageListenerContainer.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONSUMER); messageListenerContainer.setMessageListener((MessageListener) message -> { try {//from ww w. j a v a2 s. c om Destination destination = message.getJMSDestination(); if (message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; System.out.println(destination + " -> " + textMessage.getText()); } else if (message instanceof BytesMessage) { BytesMessage bytesMessage = (BytesMessage) message; byte[] bytes = new byte[(int) bytesMessage.getBodyLength()]; bytesMessage.readBytes(bytes); System.out.println(destination + " -> " + new String(bytes, Charsets.UTF_8)); } } catch (JMSException ex) { throw new UnrecoverableCoreException(ex); } }); messageListenerContainer.initialize(); messageListenerContainer.start(); System.out.println("Dumping messages from all topics"); Runtime.getRuntime().addShutdownHook(new Thread(messageListenerContainer::stop)); Thread.sleep(Long.MAX_VALUE); }
From source file:com.mirth.connect.connectors.jms.JmsMessageUtils.java
/** * @param message// w w w. j a v a 2s . c om * the message to receive the bytes from. Note this only works * for TextMessge, ObjectMessage, StreamMessage and BytesMessage. * @return a byte array corresponding with the message payload * @throws JMSException * if the message can't be read or if the message passed is a * MapMessage * @throws java.io.IOException * if a failiare occurs while stream and converting the message * data */ public static byte[] getBytesFromMessage(Message message) throws JMSException, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024 * 2]; int len; if (message instanceof BytesMessage) { BytesMessage bMsg = (BytesMessage) message; // put message in read-only mode bMsg.reset(); while ((len = bMsg.readBytes(buffer)) != -1) { baos.write(buffer, 0, len); } } else if (message instanceof StreamMessage) { StreamMessage sMsg = (StreamMessage) message; sMsg.reset(); while ((len = sMsg.readBytes(buffer)) != -1) { baos.write(buffer, 0, len); } } else if (message instanceof ObjectMessage) { ObjectMessage oMsg = (ObjectMessage) message; ByteArrayOutputStream bs = new ByteArrayOutputStream(); ObjectOutputStream os = new ObjectOutputStream(bs); os.writeObject(oMsg.getObject()); os.flush(); baos.write(bs.toByteArray()); os.close(); bs.close(); } else if (message instanceof TextMessage) { TextMessage tMsg = (TextMessage) message; baos.write(tMsg.getText().getBytes()); } else { throw new JMSException("Cannot get bytes from Map Message"); } baos.flush(); byte[] bytes = baos.toByteArray(); baos.close(); return bytes; }
From source file:com.mirth.connect.connectors.jms.JmsMessageUtils.java
public static Object getObjectForMessage(Message source) throws JMSException { Object result = null;//from w ww . j a va2 s. co m try { if (source instanceof ObjectMessage) { result = ((ObjectMessage) source).getObject(); } else if (source instanceof MapMessage) { Hashtable map = new Hashtable(); MapMessage m = (MapMessage) source; for (Enumeration e = m.getMapNames(); e.hasMoreElements();) { String name = (String) e.nextElement(); Object obj = m.getObject(name); map.put(name, obj); } result = map; } else if (source instanceof javax.jms.BytesMessage) { javax.jms.BytesMessage bm = (javax.jms.BytesMessage) source; java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream(); byte[] buffer = new byte[1024 * 4]; int len = 0; bm.reset(); while ((len = bm.readBytes(buffer)) != -1) { baos.write(buffer, 0, len); } baos.flush(); result = baos.toByteArray(); baos.close(); if (result != null) { if (logger.isDebugEnabled()) logger.debug("JMSToObject: extracted " + ((byte[]) result).length + " bytes from JMS BytesMessage"); } } else if (source instanceof TextMessage) { result = ((TextMessage) source).getText(); } else if (source instanceof BytesMessage) { byte[] bytes = getBytesFromMessage(source); return CompressionHelper.uncompressByteArray(bytes); } else if (source instanceof StreamMessage) { StreamMessage sm = (javax.jms.StreamMessage) source; result = new java.util.Vector(); try { Object obj = null; while ((obj = sm.readObject()) != null) { ((java.util.Vector) result).addElement(obj); } } catch (MessageEOFException eof) { } catch (Exception e) { throw new JMSException("Failed to extract information from JMS Stream Message: " + e); } } else { result = source; } } catch (Exception e) { throw new JMSException("Failed to transform message: " + e.getMessage()); } return result; }
From source file:com.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingWorker.java
public static String getMessageBody(Message message) { try {/* w ww .j av a 2s.c o m*/ if (message instanceof MapMessage) { MapMessage mm = (MapMessage) message; ObjectMapper mapper = new ObjectMapper(); ObjectNode root = mapper.createObjectNode(); @SuppressWarnings("unchecked") Enumeration<String> e = mm.getMapNames(); while (e.hasMoreElements()) { String field = e.nextElement(); root.set(field, mapper.convertValue(mm.getObject(field), JsonNode.class)); } return mapper.writer().writeValueAsString(root); } else if (message instanceof TextMessage) { TextMessage tm = (TextMessage) message; return tm.getText(); } else if (message instanceof BytesMessage) { BytesMessage bm = (BytesMessage) message; byte[] bytes = new byte[(int) bm.getBodyLength()]; if (bm.readBytes(bytes) == bm.getBodyLength()) { return new String(bytes); } } else { log.log(Level.SEVERE, "Unsupported message type:\n" + formatMessage(message)); } } catch (Exception e) { log.log(Level.SEVERE, "Unhandled exception retrieving message body:\n" + formatMessage(message), e); } return ""; }
From source file:com.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingWorker.java
public static String formatMessage(Message message) { StringBuilder sb = new StringBuilder(); try {//from w w w . ja v a 2s . com String headers = formatHeaders(message); if (headers.length() > 0) { sb.append("Message Headers:\n"); sb.append(headers); } sb.append("Message Properties:\n"); @SuppressWarnings("unchecked") Enumeration<String> propNames = message.getPropertyNames(); while (propNames.hasMoreElements()) { String propertyName = propNames.nextElement(); sb.append(" "); sb.append(propertyName); sb.append(": "); if (message.getObjectProperty(propertyName) != null) { sb.append(message.getObjectProperty(propertyName).toString()); } sb.append("\n"); } sb.append("Message Content:\n"); if (message instanceof TextMessage) { sb.append(((TextMessage) message).getText()); } else if (message instanceof MapMessage) { MapMessage mm = (MapMessage) message; ObjectMapper mapper = new ObjectMapper(); ObjectNode root = mapper.createObjectNode(); @SuppressWarnings("unchecked") Enumeration<String> e = mm.getMapNames(); while (e.hasMoreElements()) { String field = e.nextElement(); root.put(field, mapper.convertValue(mm.getObject(field), JsonNode.class)); } sb.append(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root)); } else if (message instanceof BytesMessage) { BytesMessage bm = (BytesMessage) message; bm.reset(); byte[] bytes = new byte[(int) bm.getBodyLength()]; if (bm.readBytes(bytes) == bm.getBodyLength()) { sb.append(new String(bytes)); } } else { sb.append(" Unhandled message type: " + message.getJMSType()); } } catch (Exception e) { log.log(Level.SEVERE, "Unable to format message:", e); } return sb.toString(); }
From source file:com.atlantbh.jmeter.plugins.jmstools.BinaryMessageConverter.java
@Override public Object fromMessage(Message message) throws JMSException, MessageConversionException { Enumeration<String> names = message.getPropertyNames(); messageProperties = new HashMap<String, String>(); while (names.hasMoreElements()) { String name = names.nextElement(); messageProperties.put(name, message.getStringProperty(name)); }/* ww w . ja va 2 s . c o m*/ BytesMessage bm = (BytesMessage) message; byte[] transfer = new byte[(int) bm.getBodyLength()]; bm.readBytes(transfer); return new String(transfer); }
From source file:com.espertech.esper.example.servershell.SampleJMSMessageListener.java
private String getBody(BytesMessage bytesMsg) { try {//from www . jav a 2s .c o m long length = bytesMsg.getBodyLength(); byte[] buf = new byte[(int) length]; bytesMsg.readBytes(buf); return new String(buf); } catch (JMSException e) { String text = "Error getting message body"; log.error(text, e); throw new RuntimeException(text, e); } }
From source file:org.dataminx.dts.ws.jms.DtsWsMessageConverter.java
/** * Extracts the given JMS Message payload and returns it as an object. * * @param message the incoming JMS message * @return the message payload as an {@link Object} * @throws JMSException if the incoming message is not of a supported * message type/* w w w .ja v a 2 s.c o m*/ */ private Object extractMessagePayload(final Message message) throws JMSException { final Object payload; if (message instanceof TextMessage) { final TextMessage textMessage = (TextMessage) message; payload = textMessage.getText(); } else if (message instanceof ObjectMessage) { final ObjectMessage objectMessage = (ObjectMessage) message; payload = objectMessage.getObject(); } else if (message instanceof BytesMessage) { final BytesMessage bytesMessage = (BytesMessage) message; final byte[] bytes = new byte[(int) bytesMessage.getBodyLength()]; bytesMessage.readBytes(bytes); final ByteArrayInputStream bis = new ByteArrayInputStream(bytes); payload = new StreamSource(bis); } else { throw new MessageConversionException("Invalid message type..."); } return payload; }
From source file:com.jim.im.group.rpc.UserRegisterListener.java
@Override public void onMessage(Message arg0) { BytesMessage bytesmsg = (BytesMessage) arg0; int length;// w w w . j av a2 s . co m try { length = (int) bytesmsg.getBodyLength(); byte[] bytes = new byte[length]; bytesmsg.readBytes(bytes); // ProtocolStream stream = new ProtocolStream(bytes); // registerobj obj = registerobjSerializer.getregisterobj(stream); User user = new User(); Integer usernumber = 1; user.setGatewayUserId(usernumber); user.setAppId("sfa"); user.setTenantId("hengda"); userService.createUser(user); logger.info("UserRegisterListener.onMessage arrived user :" + usernumber); } catch (JMSException e) { e.printStackTrace(); } catch (ImException e) { e.printStackTrace(); } }
From source file:org.globus.crux.messaging.utils.JAXBMessageConverter.java
/** * <p>/*from w w w .j a va 2 s . c om*/ * Unmarshal given <code>message</code> into an <code>object</code>. * </p> * * <p> * Should we raise an exception if the XML message encoding is not in sync with the underlying TextMessage encoding when the JMS * Provider supports MOM message's encoding ? * </p> * * @param message * message to unmarshal, MUST be an instance of {@link TextMessage} or of {@link BytesMessage}. * @see org.springframework.jms.support.converter.MessageConverter#fromMessage(javax.jms.Message) */ public Object fromMessage(Message message) throws JMSException, MessageConversionException { Source source; if (message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; source = new StreamSource(new StringReader(textMessage.getText())); } else if (message instanceof BytesMessage) { BytesMessage bytesMessage = (BytesMessage) message; byte[] bytes = new byte[(int) bytesMessage.getBodyLength()]; bytesMessage.readBytes(bytes); source = new StreamSource(new ByteArrayInputStream(bytes)); } else { throw new MessageConversionException("Unsupported JMS Message type " + message.getClass() + ", expected instance of TextMessage or BytesMessage for " + message); } Object result; try { result = context.createUnmarshaller().unmarshal(source); } catch (JAXBException e) { throw new MessageConversionException("Error unmarshalling message", e); } return result; }