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.mot.common.mq.ActiveMQFactory.java

/**
 * @param tick/*w  w w.ja va 2 s .co  m*/
 * @param ttl
 * @param persistent
 */
public void publishTick(Tick tick, MessageProducer mp) {

    BytesMessage tickMessage;
    try {
        Session session = this.createSession();

        // Create a new ByteMessage
        tickMessage = session.createBytesMessage();

        // Serialize the tick content into the message
        tickMessage.writeBytes(tick.serialize());
        tickMessage.setStringProperty("Symbol", tick.getSymbol());
        tickMessage.setStringProperty("Currency", tick.getCurrency());

        mp.send(tickMessage);
        this.closeSession(session);
    } catch (JMSException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.mot.common.mq.ActiveMQFactory.java

/**
 * /*from  w w  w . jav a 2  s . c o m*/
 * @param tick
 * @param ttl
 * @param persistent
 */
public void publishTicks(Tick[] tick, MessageProducer mp) {

    BytesMessage tickMessage;
    try {
        Session session = this.createSession();

        for (int i = 0; i < tick.length; i++) {
            // Create a new ByteMessage
            tickMessage = session.createBytesMessage();

            // Serialize the tick content into the message
            tickMessage.writeBytes(tick[i].serialize());
            tickMessage.setStringProperty("Symbol", tick[i].getSymbol());
            tickMessage.setStringProperty("Currency", tick[i].getCurrency());

            mp.send(tickMessage);
        }
        this.closeSession(session);
    } catch (JMSException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.mot.common.mq.ActiveMQFactory.java

/**
 * @param tickSize//  w  w  w .  j a  v a2s  .com
 * @param ttl
 * @param persistent
 */
public void publishTickSize(TickSize tickSize, MessageProducer mp) {

    BytesMessage tickMessage;
    try {
        Session session = this.createSession();

        // Create a new ByteMessage
        tickMessage = session.createBytesMessage();

        // Serialize the tick content into the message
        tickMessage.writeBytes(tickSize.serialize());
        tickMessage.setStringProperty("Symbol", tickSize.getSymbol());

        mp.send(tickMessage);
        this.closeSession(session);
    } catch (JMSException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.mot.common.mq.ActiveMQFactory.java

/**
 * @param order - Order to be placed// www. j a v  a 2s.  co m
 * @param ttl - Time to live. Set to 0 for indefinite
 * @param deliveryMode - Set to 1 for non-persistent. Default 0
 */
public void publishOrder(Order order, MessageProducer mp) {
    BytesMessage orderMessage;
    try {
        Session session = this.createSession();

        // Create a new ByteMessage
        orderMessage = session.createBytesMessage();

        // Serialize the tick content into the message
        orderMessage.writeBytes(order.serialize());
        orderMessage.setStringProperty("Symbol", order.getSymbol());

        mp.send(orderMessage);
        this.closeSession(session);
    } catch (JMSException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.mot.common.mq.ActiveMQFactory.java

/**
 * Publish a simulation request//from   ww w  .j  ava  2  s . c o  m
 * 
 * @param sr
 * @param ttl
 * @param persistent
 */
public void publishSimulationRequest(SimulationRequest sr, MessageProducer mp) {

    BytesMessage simulationMessage;
    try {
        Session session = this.createSession();

        // Create a new ByteMessage
        simulationMessage = session.createBytesMessage();

        // Serialize the simulation request content into the message
        simulationMessage.writeBytes(sr.serialize());
        simulationMessage.setStringProperty("Symbol", sr.getSymbol());
        simulationMessage.setStringProperty("LoadValues", String.valueOf(sr.getLoadValues()));
        simulationMessage.setStringProperty("StartDate", String.valueOf(sr.getStartDate()));
        simulationMessage.setStringProperty("EndDate", String.valueOf(sr.getEndDate()));

        mp.send(simulationMessage);
        this.closeSession(session);
    } catch (JMSException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.mot.common.mq.ActiveMQFactory.java

/**
 * Publish a tick history object/* w w w. ja v a2 s .  c o  m*/
 * @param tick
 */
public void publishTickHistory(TickHistory tick, MessageProducer mp) {

    BytesMessage tickMessage;
    try {
        Session session = this.createSession();

        // Create a new ByteMessage
        tickMessage = session.createBytesMessage();

        // Serialize the tick content into the message
        tickMessage.writeBytes(tick.serialize());
        mp.send(tickMessage);
        this.closeSession(session);
    } catch (JMSException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.mule.transport.jms.integration.JmsTransformersTestCase.java

@Test
public void testCompressedBytesMessage() throws Exception {
    RequestContext.setEvent(getTestEvent("test"));

    // use GZIP/*from w ww. ja v  a  2s. c  o m*/
    CompressionStrategy compressor = new GZipCompression();

    // create compressible data
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    for (int i = 0; i < 5000; i++) {
        baos.write(i);
    }

    byte[] originalBytes = baos.toByteArray();
    byte[] compressedBytes = compressor.compressByteArray(originalBytes);
    assertTrue("Source compressedBytes should be compressed", compressor.isCompressed(compressedBytes));

    // now create a BytesMessage from the compressed byte[]
    AbstractJmsTransformer trans = new SessionEnabledObjectToJMSMessage(session);
    trans.setReturnDataType(DataTypeFactory.create(BytesMessage.class));
    initialiseObject(trans);
    Object result2 = trans.transform(compressedBytes);
    assertTrue("Transformed object should be a Bytes message", result2 instanceof BytesMessage);

    // check whether the BytesMessage contains the compressed bytes
    BytesMessage intermediate = (BytesMessage) result2;
    intermediate.reset();
    byte[] intermediateBytes = new byte[(int) (intermediate.getBodyLength())];
    int intermediateSize = intermediate.readBytes(intermediateBytes);
    assertTrue("Intermediate bytes must be compressed", compressor.isCompressed(intermediateBytes));
    assertTrue("Intermediate bytes must be equal to compressed source",
            Arrays.equals(compressedBytes, intermediateBytes));
    assertEquals("Intermediate bytes and compressed source must have same size", compressedBytes.length,
            intermediateSize);

    // now test the other way around: getting the byte[] from a manually created
    // BytesMessage
    AbstractJmsTransformer trans2 = createObject(JMSMessageToObject.class);
    trans2.setReturnDataType(DataTypeFactory.BYTE_ARRAY);
    BytesMessage bMsg = session.createBytesMessage();
    bMsg.writeBytes(compressedBytes);
    Object result = trans2.transform(bMsg);
    assertTrue("Transformed object should be a byte[]", result instanceof byte[]);
    assertTrue("Result should be compressed", compressor.isCompressed((byte[]) result));
    assertTrue("Source and result should be equal", Arrays.equals(compressedBytes, (byte[]) result));
}

From source file:org.mule.transport.jms.JmsMessageUtils.java

private static Message byteArrayToMessage(byte[] value, Session session) throws JMSException {
    BytesMessage bMsg = session.createBytesMessage();
    bMsg.writeBytes(value);

    return bMsg;/*from   w  ww.  j  ava2 s .c  o  m*/
}

From source file:org.mule.transport.jms.JmsMessageUtils.java

private static Message outputHandlerToMessage(OutputHandler value, Session session) throws JMSException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {/*from w  w  w.j  ava  2  s  .c  om*/
        value.write(null, output);
    } catch (IOException e) {
        JMSException j = new JMSException("Could not serialize OutputHandler.");
        j.initCause(e);
        throw j;
    }

    BytesMessage bMsg = session.createBytesMessage();
    bMsg.writeBytes(output.toByteArray());

    return bMsg;
}

From source file:org.opencastproject.message.broker.impl.MessageSenderImpl.java

/**
 * A common function for sending all types of messages that this class handles. Only one Message or one message
 * payload can be provided.// www.j av  a  2 s.c o  m
 *
 * @param destinationId
 *          The id of the queue or topic.
 * @param type
 *          The type of the destination either queue or topic.
 * @param createdMessage
 *          An optional user created message.
 * @param messageText
 *          An optional text payload for a message.
 * @param messageBytes
 *          An optional byte[] payload.
 * @param offset
 *          An optional offset for a byte[] payload.
 * @param length
 *          An optional length of bytes to add to the message byte[] payload.
 */
private void send(String destinationId, DestinationType type, Option<Message> createdMessage,
        Option<String> messageText, Option<byte[]> messageBytes, Option<Integer> offset, Option<Integer> length,
        Option<Serializable> messageObject) {
    try {

        // Create a message or use the provided one.
        Message message = null;
        if (createdMessage.isSome()) {
            message = createdMessage.get();
        } else if (messageText.isSome()) {
            message = getSession().createTextMessage(messageText.get());
        } else if (messageBytes.isSome() && offset.isNone() && length.isNone()) {
            BytesMessage bytesMessage = getSession().createBytesMessage();
            bytesMessage.writeBytes(messageBytes.get());
            message = bytesMessage;
        } else if (messageBytes.isSome() && offset.isSome() && length.isSome()) {
            BytesMessage bytesMessage = getSession().createBytesMessage();
            bytesMessage.writeBytes(messageBytes.get(), offset.get(), length.get());
            message = bytesMessage;
        } else if (messageObject.isSome()) {
            final Organization organization = securityService.getOrganization();
            final User user = securityService.getUser();
            final BaseMessage baseMessage = new BaseMessage(organization, user, messageObject.get());
            message = getSession().createObjectMessage(baseMessage);
        } else {
            throw new IllegalArgumentException("To send a message there must be a message payload specified!");
        }

        Destination destination;
        // Create the destination (Topic or Queue)
        if (type.equals(DestinationType.Queue)) {
            destination = getSession().createQueue(destinationId);
        } else {
            destination = getSession().createTopic(destinationId);
        }

        // Tell the producer to send the message
        logger.trace("Sent message: " + message.hashCode() + " : " + Thread.currentThread().getName());

        // Send the message
        getMessageProducer().send(destination, message);
    } catch (JMSException e) {
        logger.error("Had an exception while trying to send a message {}", ExceptionUtils.getStackTrace(e));
    }

}