Example usage for javax.jms BytesMessage writeBytes

List of usage examples for javax.jms BytesMessage writeBytes

Introduction

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

Prototype


void writeBytes(byte[] value) throws JMSException;

Source Link

Document

Writes a byte array to the bytes message stream.

Usage

From source file:org.springframework.jms.support.converter.obm.MarshallingMessageConverter.java

protected BytesMessage marshalToBytesMessage(Object object, Session session,
        org.springframework.obm.Marshaller marshaller) throws JMSException, IOException, XmlMappingException {
    Assert.notNull(object);//from w ww .  j  a  va 2 s  .  co  m
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    BytesMessage message;
    try {

        marshaller.marshal(object, bos);

        message = session.createBytesMessage();
        message.writeBytes(bos.toByteArray());

        if (log.isDebugEnabled()) {
            log.debug("sent:" + object);
        }

    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return message;
}

From source file:org.wso2.carbon.registry.caching.invalidator.connection.JMSNotification.java

@Override
public void publish(Object message) {
    Session pubSession = null;/*w ww  . j  a v a 2s  .c om*/
    try {
        if (connection != null) {
            pubSession = connection.createSession(false, TopicSession.AUTO_ACKNOWLEDGE);
            MessageProducer publisher = pubSession.createProducer(destination);
            BytesMessage bytesMessage = pubSession.createBytesMessage();
            bytesMessage.writeBytes((byte[]) message);
            publisher.send(bytesMessage);
        }
    } catch (JMSException e) {
        log.error("Global cache invalidation: Error in publishing the message", e);
    } finally {
        if (pubSession != null) {
            try {
                pubSession.close();
            } catch (JMSException e) {
                log.error("Global cache invalidation: Error in publishing the message", e);
            }
        }
    }
}

From source file:org.wso2.esb.integration.common.utils.clients.jmsclient.JMSQueueMessageProducer.java

/**
 * Method to send a BytesMessage.//from  www .ja va2 s . c  o m
 *
 * @param payload content of the BytesMessage to be sent
 * @throws JMSException if an error occurs sending the BytesMessage
 */
public void sendBytesMessage(byte[] payload) throws JMSException {
    checkIfConnected();
    BytesMessage bytesMessage = session.createBytesMessage();
    bytesMessage.writeBytes(payload);
    producer.send(bytesMessage);
}

From source file:tools.ProducerTool.java

@Override
public void run() {
    Connection connection = null;
    Session session = null;//from   w  ww .  jav  a  2 s  . c o  m
    try {
        connection = connectionFactory.createConnection();
        if (clientId != null) {
            connection.setClientID(clientId);
        }
        session = connection.createSession(transacted, acknowledgeMode);
        Destination destination = null;
        if (jndiLookupDestinations) {
            destination = (Destination) context.lookup(destinationName);
        } else {
            if (useQueueDestinations) {
                if (useTemporaryDestinations) {
                    destination = session.createTemporaryQueue();
                } else {
                    destination = session.createQueue(destinationName);
                }
            } else {
                if (useTemporaryDestinations) {
                    destination = session.createTemporaryTopic();
                } else {
                    destination = session.createTopic(destinationName);
                }
            }
        }

        MessageProducer producer = session.createProducer(destination);
        if (durable) {
            producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        } else {
            producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        }

        int numMessagesToSend = useFinalControlMessage ? numMessages - 1 : numMessages;

        for (int i = 0; i < numMessagesToSend; i++) {
            String messageText = "Message " + i + " at " + new Date();
            if (bytesLength > -1) {
                byte[] messageTextBytes = messageText.getBytes(StandardCharsets.UTF_8);
                BytesMessage bytesMessage = session.createBytesMessage();
                bytesMessage.writeBytes(messageTextBytes);
                if (messageTextBytes.length < bytesLength) {
                    byte[] paddingBytes = new byte[bytesLength - messageTextBytes.length];
                    bytesMessage.writeBytes(paddingBytes);
                }
                if (messageGroupId != null) {
                    bytesMessage.setStringProperty("JMSXGroupID", messageGroupId);
                }
                LOGGER.info("Sending bytes message");
                producer.send(bytesMessage);
            } else {
                TextMessage textMessage = session.createTextMessage(messageText);
                if (messageGroupId != null) {
                    textMessage.setStringProperty("JMSXGroupID", messageGroupId);
                }
                LOGGER.info("Sending text message: " + messageText);
                producer.send(textMessage);
            }

            if (perMessageSleepMS > 0) {
                Thread.sleep(perMessageSleepMS);
            }
            if (transacted) {
                if ((i + 1) % batchSize == 0) {
                    session.commit();
                }
            }
        }
        if (useFinalControlMessage) {
            Message message = session.createMessage();
            if (messageGroupId != null) {
                message.setStringProperty("JMSXGroupID", messageGroupId);
            }
            LOGGER.info("Sending message");
            producer.send(message);
            if (transacted) {
                session.commit();
            }
        }
        producer.close();
    } catch (Exception ex) {
        LOGGER.error("ProducerTool hit exception: " + ex.getMessage(), ex);
    } finally {
        if (session != null) {
            try {
                session.close();
            } catch (JMSException e) {
                LOGGER.error("JMSException closing session", e);
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                LOGGER.error("JMSException closing session", e);
            }
        }
    }
}