Example usage for javax.jms BytesMessage readBytes

List of usage examples for javax.jms BytesMessage readBytes

Introduction

In this page you can find the example usage for javax.jms BytesMessage readBytes.

Prototype


int readBytes(byte[] value) throws JMSException;

Source Link

Document

Reads a byte array from the bytes message stream.

Usage

From source file:fr.xebia.springframework.jms.support.converter.JaxbMessageConverter.java

/**
 * <p>//from   w  w  w .j a  v  a  2 s.  co  m
 * 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//from   www  .  j a v  a 2s.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);
    }
}

From source file:drepcap.frontend.jms.JmsAdapter.java

public void startReceiveData() throws JMSException {
    dataTopic = session.createTopic(componentName + ".data");
    dataConsumer = session.createConsumer(dataTopic);
    dataConsumer.setMessageListener(new MessageListener() {
        @Override//from  ww  w . j a v  a 2s .  c o m
        public void onMessage(Message msg) {
            for (ByteArrayReceiver baDataReceiver : byteArrayDataReceivers) {
                if (baDataReceiver != null && msg instanceof BytesMessage) {
                    BytesMessage bMsg = (BytesMessage) msg;
                    try {
                        byte[] data = new byte[(int) bMsg.getBodyLength()];
                        bMsg.readBytes(data);
                        baDataReceiver.process(data);
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }

            for (ObjectReceiver objReceiver : objectReceivers) {
                if (objReceiver != null && msg instanceof ObjectMessage) {
                    ObjectMessage objMsg = (ObjectMessage) msg;
                    try {
                        objReceiver.process(objMsg.getObject());
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    });
}

From source file:org.bremersee.common.jms.DefaultJmsConverter.java

private Object tryToGetPayload(BytesMessage msg, Class<?> payloadClass) throws JMSException {
    int len;//from  www.ja v  a 2  s  .  c  o m
    byte[] buf = new byte[1024];
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    while ((len = msg.readBytes(buf)) != -1) {
        out.write(buf, 0, len);
    }
    byte[] payloadBytes = out.toByteArray();
    if (payloadBytes == null || payloadBytes.length == 0) {
        return null;
    }

    Object payload = null;
    if (shouldTryJson(payloadClass, payloadBytes)) {
        try {
            payload = objectMapper.readValue(payloadBytes, payloadClass);

        } catch (Throwable t0) { // NOSONAR
            log.info("Reading JSON from message with JMSType [" + msg.getJMSType() + "] failed.");
        }
    }

    if (shouldTryUnmarshal(payload, payloadClass)) {
        try {
            payload = marshaller.unmarshal(new StreamSource(new ByteArrayInputStream(payloadBytes)));

        } catch (Throwable t0) { // NOSONAR
            log.info("Reading XML from message with JMSType [" + msg.getJMSType() + "] failed.");
        }
    }

    if (shouldTryDeserialize(payload, payloadClass)) {
        try {
            ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(payloadBytes));
            payload = objectInputStream.readObject();

        } catch (Throwable t0) { // NOSONAR
            log.info("Reading serialized object of JMSType [" + msg.getJMSType() + "] failed.");
        }
    }

    if (payload == null) {
        MessageConversionException e = new MessageConversionException("Converting BytesMessage failed.");
        log.error("Could not convert bytes:\n" + Base64.encodeBase64String(payloadBytes) + "\n", e);
        throw e;
    }

    return payload;
}

From source file:com.kinglcc.spring.jms.core.converter.Jackson2JmsMessageConverter.java

/**
 * Convert a BytesMessage to a Java Object with the specified type.
 * @param message the input message/*from  www.  ja va2 s .  com*/
 * @return the message body
 * @throws JMSException if thrown by JMS
 * @throws IOException in case of I/O errors
 */
protected String getPayloadFromBytesMessage(BytesMessage message) throws JMSException, IOException {

    String encoding = this.encoding;
    if (this.encodingPropertyName != null && message.propertyExists(this.encodingPropertyName)) {
        encoding = message.getStringProperty(this.encodingPropertyName);
    }
    byte[] bytes = new byte[(int) message.getBodyLength()];
    message.readBytes(bytes);
    try {
        return new String(bytes, encoding);
    } catch (UnsupportedEncodingException ex) {
        throw new MessageConversionException("Cannot convert bytes to String", ex);
    }
}

From source file:com.bitsofproof.supernode.core.ImplementBCSAPI.java

private void addTransactionRequestListener() throws JMSException {
    addMessageListener("transactionRequest", new MessageListener() {
        @Override//from w w  w  .ja  va2 s .c om
        public void onMessage(Message arg0) {
            BytesMessage o = (BytesMessage) arg0;
            try {
                byte[] body = new byte[(int) o.getBodyLength()];
                o.readBytes(body);
                String hash = new Hash(BCSAPIMessage.Hash.parseFrom(body).getHash(0).toByteArray()).toString();
                Transaction t = getTransaction(hash);
                if (t != null) {
                    reply(o.getJMSReplyTo(), t.toProtobuf().toByteArray());
                } else {
                    reply(o.getJMSReplyTo(), null);
                }
            } catch (Exception e) {
                log.trace("Rejected invalid transaction request ", e);
            }
        }
    });
}

From source file:com.bitsofproof.supernode.core.ImplementBCSAPI.java

private void addBlockrequestListener() throws JMSException {
    addMessageListener("blockRequest", new MessageListener() {
        @Override/*  ww  w.j  a  v a2s  .com*/
        public void onMessage(Message arg0) {
            BytesMessage o = (BytesMessage) arg0;
            try {
                byte[] body = new byte[(int) o.getBodyLength()];
                o.readBytes(body);
                String hash = new Hash(BCSAPIMessage.Hash.parseFrom(body).getHash(0).toByteArray()).toString();
                Block b = getBlock(hash);
                if (b != null) {
                    reply(o.getJMSReplyTo(), b.toProtobuf().toByteArray());
                } else {
                    reply(o.getJMSReplyTo(), null);
                }
            } catch (Exception e) {
                log.trace("Rejected invalid block request ", e);
            }
        }
    });
}

From source file:com.bitsofproof.supernode.core.ImplementBCSAPI.java

private void addColorRequestListener() throws JMSException {
    addMessageListener("colorRequest", new MessageListener() {
        @Override/*from   w  w w  .j a  v a  2 s. c o  m*/
        public void onMessage(Message message) {
            BytesMessage o = (BytesMessage) message;
            try {
                byte[] body = new byte[(int) o.getBodyLength()];
                o.readBytes(body);
                String hash = new Hash(BCSAPIMessage.Hash.parseFrom(body).getHash(0).toByteArray()).toString();
                Color c = getColor(hash);
                if (c != null) {
                    reply(o.getJMSReplyTo(), c.toProtobuf().toByteArray());
                } else {
                    reply(o.getJMSReplyTo(), null);
                }
            } catch (Exception e) {
                log.trace("Rejected invalid color request ", e);
            }
        }
    });
}

From source file:com.bitsofproof.supernode.core.ImplementBCSAPI.java

private void addNewBlockListener() throws JMSException {
    addMessageListener("newBlock", new MessageListener() {
        @Override//  ww  w. j  ava 2s  .c  om
        public void onMessage(Message arg0) {
            BytesMessage o = (BytesMessage) arg0;
            try {
                byte[] body = new byte[(int) o.getBodyLength()];
                o.readBytes(body);
                Block block = Block.fromProtobuf(BCSAPIMessage.Block.parseFrom(body));
                block.computeHash();
                sendBlock(block);
                reply(o.getJMSReplyTo(), null);
            } catch (Exception e) {
                BCSAPIMessage.ExceptionMessage.Builder builder = BCSAPIMessage.ExceptionMessage.newBuilder();
                builder.setBcsapiversion(1);
                builder.addMessage(e.getMessage());
                try {
                    reply(o.getJMSReplyTo(), builder.build().toByteArray());
                } catch (JMSException e1) {
                    log.error("Can not send reply ", e1);
                }
            }
        }
    });
}

From source file:com.bitsofproof.supernode.core.ImplementBCSAPI.java

private void addNewTransactionListener() throws JMSException {
    addMessageListener("newTransaction", new MessageListener() {
        @Override/*  ww  w .  j  av  a2s  .c o  m*/
        public void onMessage(Message arg0) {
            BytesMessage o = (BytesMessage) arg0;
            try {
                byte[] body = new byte[(int) o.getBodyLength()];
                o.readBytes(body);
                Transaction transaction = Transaction.fromProtobuf(BCSAPIMessage.Transaction.parseFrom(body));
                transaction.computeHash();
                sendTransaction(transaction);
                reply(o.getJMSReplyTo(), null);
            } catch (Exception e) {
                BCSAPIMessage.ExceptionMessage.Builder builder = BCSAPIMessage.ExceptionMessage.newBuilder();
                builder.setBcsapiversion(1);
                builder.addMessage(e.getMessage());
                try {
                    reply(o.getJMSReplyTo(), builder.build().toByteArray());
                } catch (JMSException e1) {
                    log.error("Can not send reply ", e1);
                }
            }
        }
    });
}