Example usage for javax.jms BytesMessage setStringProperty

List of usage examples for javax.jms BytesMessage setStringProperty

Introduction

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

Prototype


void setStringProperty(String name, String value) throws JMSException;

Source Link

Document

Sets a String property value with the specified name into the message.

Usage

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

/**
 * Map the given object to a {@link BytesMessage}.
 * @param object the object to be mapped
 * @param session current JMS session//from  www .  j  a  v a 2s.  c  o m
 * @param objectMapper the mapper to use
 * @return the resulting message
 * @throws JMSException if thrown by JMS methods
 * @throws IOException in case of I/O errors
 * @see Session#createBytesMessage
 */
protected BytesMessage mapToBytesMessage(Object object, Session session, ObjectMapper objectMapper)
        throws JMSException, IOException {

    ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
    OutputStreamWriter writer = new OutputStreamWriter(bos, this.encoding);
    objectMapper.writeValue(writer, object);

    BytesMessage message = session.createBytesMessage();
    message.writeBytes(bos.toByteArray());
    if (this.encodingPropertyName != null) {
        message.setStringProperty(this.encodingPropertyName, this.encoding);
    }
    return message;
}

From source file:org.eclipse.smila.connectivity.queue.worker.internal.task.impl.Send.java

/**
 * Creates message./*from   ww w  .j a  va 2s .  co m*/
 * 
 * @param config
 *          the config
 * @param record
 *          the record
 * @param messageProperties
 *          the jms message properties
 * @param session
 *          the session
 * 
 * @return the message
 * 
 * @throws JMSException
 *           the JMS exception
 */
private Message createMessage(final SendType config, final Record record, final Properties messageProperties,
        final Session session) throws JMSException {
    final BytesMessage message = session.createBytesMessage();

    // set dynamic message properties
    if (messageProperties != null) {
        final Enumeration<?> propertyNames = messageProperties.propertyNames();
        while (propertyNames.hasMoreElements()) {
            final String name = (String) propertyNames.nextElement();
            message.setStringProperty(name, messageProperties.getProperty(name));
        }
    }

    // get static properties of config file
    for (final PropertyType property : config.getSetProperty()) {
        message.setStringProperty(property.getName(), property.getValue());
    }

    final byte[] serialized = XmlSerializationUtils.serialize2byteArray(record);
    message.writeBytes(serialized);
    return message;
}

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

/**
 * @param tick//w  w  w . j a v a2s . c o 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   www.  j  a v a 2s  .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 va 2s. c om*/
 * @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// w  ww  .ja  v a 2 s  .  c  om
 * @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/* w  w w . j a  v  a  2 s.co  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:tools.ProducerTool.java

@Override
public void run() {
    Connection connection = null;
    Session session = null;//from   w ww . j  a va2 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);
            }
        }
    }
}