List of usage examples for javax.jms BytesMessage getBodyLength
long getBodyLength() 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 w w w.j av a2s . c o m*/ 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.redhat.jenkins.plugins.ci.messaging.ActiveMqMessagingWorker.java
public static String getMessageBody(Message message) { try {/* ww w . 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 ww w .j ava2 s . 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.espertech.esper.example.servershell.SampleJMSMessageListener.java
private String getBody(BytesMessage bytesMsg) { try {/*from ww w.j av a2 s . 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: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)); }// www.j a v a2 s .c om BytesMessage bm = (BytesMessage) message; byte[] transfer = new byte[(int) bm.getBodyLength()]; bm.readBytes(transfer); return new String(transfer); }
From source file:com.jim.im.group.rpc.UserRegisterListener.java
@Override public void onMessage(Message arg0) { BytesMessage bytesmsg = (BytesMessage) arg0; int length;/* ww w . j a v a2 s. c o 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.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.j a va 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:org.globus.crux.messaging.utils.JAXBMessageConverter.java
/** * <p>//from ww w . jav a2 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; }
From source file:fr.xebia.springframework.jms.support.converter.JaxbMessageConverter.java
/** * <p>// www. jav a2s . 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) * @see org.springframework.oxm.Unmarshaller#unmarshal(Source) */ public Object fromMessage(Message message) throws JMSException, MessageConversionException { Source source; if (message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; source = new StringSource(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 = jaxb2Marshaller.unmarshal(source); return result; }
From source file:com.jkoolcloud.tnt4j.streams.parsers.ActivityJMSMessageParser.java
/** * Parse JMS {@link BytesMessage} activity info into activity data map. * * @param bytesMessage// www . j av a 2 s . c om * JMS bytes message * @param dataMap * activity data map collected from JMS {@link BytesMessage} * @throws JMSException * if JMS exception occurs while reading bytes from message. */ protected void parseBytesMessage(BytesMessage bytesMessage, Map<String, Object> dataMap) throws JMSException { byte[] bytes = new byte[(int) bytesMessage.getBodyLength()]; bytesMessage.readBytes(bytes); if (ArrayUtils.isNotEmpty(bytes)) { dataMap.put(StreamsConstants.ACTIVITY_DATA_KEY, convertToString ? Utils.getString(bytes) : bytes); } }