Example usage for javax.jms ObjectMessage setJMSCorrelationID

List of usage examples for javax.jms ObjectMessage setJMSCorrelationID

Introduction

In this page you can find the example usage for javax.jms ObjectMessage setJMSCorrelationID.

Prototype


void setJMSCorrelationID(String correlationID) throws JMSException;

Source Link

Document

Sets the correlation ID for the message.

Usage

From source file:com.cws.esolutions.core.utils.MQUtils.java

/**
 * Puts an MQ message on a specified queue and returns the associated
 * correlation ID for retrieval upon request.
 *
 * @param connName - The connection name to utilize
 * @param authData - The authentication data to utilize, if required
 * @param requestQueue - The request queue name to put the message on
 * @param targetHost - The target host for the message
 * @param value - The data to place on the request. MUST be <code>Serialiable</code>
 * @return <code>String</code> - the JMS correlation ID associated with the message
 * @throws UtilityException {@link com.cws.esolutions.core.utils.exception.UtilityException} if an error occurs processing
 *///from   w  w  w . j a  v  a2  s.  c o m
public static final synchronized String sendMqMessage(final String connName, final List<String> authData,
        final String requestQueue, final String targetHost, final Serializable value) throws UtilityException {
    final String methodName = MQUtils.CNAME
            + "sendMqMessage(final String connName, final List<String> authData, final String requestQueue, final String targetHost, final Serializable value) throws UtilityException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Value: {}", connName);
        DEBUGGER.debug("Value: {}", requestQueue);
        DEBUGGER.debug("Value: {}", targetHost);
        DEBUGGER.debug("Value: {}", value);
    }

    Connection conn = null;
    Session session = null;
    Context envContext = null;
    InitialContext initCtx = null;
    MessageProducer producer = null;
    ConnectionFactory connFactory = null;

    final String correlationId = RandomStringUtils.randomAlphanumeric(64);

    if (DEBUG) {
        DEBUGGER.debug("correlationId: {}", correlationId);
    }

    try {
        try {
            initCtx = new InitialContext();
            envContext = (Context) initCtx.lookup(MQUtils.INIT_CONTEXT);

            connFactory = (ConnectionFactory) envContext.lookup(connName);
        } catch (NamingException nx) {
            // we're probably not in a container
            connFactory = new ActiveMQConnectionFactory(connName);
        }

        if (DEBUG) {
            DEBUGGER.debug("ConnectionFactory: {}", connFactory);
        }

        if (connFactory == null) {
            throw new UtilityException("Unable to create connection factory for provided name");
        }

        // Create a Connection
        conn = connFactory.createConnection(authData.get(0),
                PasswordUtils.decryptText(authData.get(1), authData.get(2), authData.get(3),
                        Integer.parseInt(authData.get(4)), Integer.parseInt(authData.get(5)), authData.get(6),
                        authData.get(7), authData.get(8)));
        conn.start();

        if (DEBUG) {
            DEBUGGER.debug("Connection: {}", conn);
        }

        // Create a Session
        session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

        if (DEBUG) {
            DEBUGGER.debug("Session: {}", session);
        }

        // Create a MessageProducer from the Session to the Topic or Queue
        if (envContext != null) {
            try {
                producer = session.createProducer((Destination) envContext.lookup(requestQueue));
            } catch (NamingException nx) {
                throw new UtilityException(nx.getMessage(), nx);
            }
        } else {
            Destination destination = session.createTopic(requestQueue);

            if (DEBUG) {
                DEBUGGER.debug("Destination: {}", destination);
            }

            producer = session.createProducer(destination);
        }

        if (producer == null) {
            throw new JMSException("Failed to create a producer object");
        }

        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

        if (DEBUG) {
            DEBUGGER.debug("MessageProducer: {}", producer);
        }

        ObjectMessage message = session.createObjectMessage(true);
        message.setJMSCorrelationID(correlationId);
        message.setStringProperty("targetHost", targetHost);

        if (DEBUG) {
            DEBUGGER.debug("correlationId: {}", correlationId);
        }

        message.setObject(value);

        if (DEBUG) {
            DEBUGGER.debug("ObjectMessage: {}", message);
        }

        producer.send(message);
    } catch (JMSException jx) {
        throw new UtilityException(jx.getMessage(), jx);
    } finally {
        try {
            // Clean up
            if (!(session == null)) {
                session.close();
            }

            if (!(conn == null)) {
                conn.close();
                conn.stop();
            }
        } catch (JMSException jx) {
            ERROR_RECORDER.error(jx.getMessage(), jx);
        }
    }

    return correlationId;
}

From source file:biz.fstechnology.micro.server.jms.ResponseMessageCreator.java

/**
 * @see org.springframework.jms.core.MessageCreator#createMessage(javax.jms.Session)
 */// w w  w  . j av a  2s  . c o m
@Override
public Message createMessage(Session session) throws JMSException {
    ObjectMessage message = session.createObjectMessage();
    message.setObject(getContents());
    message.setJMSCorrelationID(getRequestId());
    return message;

}

From source file:org.osgp.adapter.protocol.dlms.infra.messaging.OsgpRequestMessageSender.java

public void send(final RequestMessage requestMessage, final String messageType) {
    LOGGER.info("Sending request message to OSGP.");

    this.osgpRequestsJmsTemplate.send(new MessageCreator() {

        @Override//w  w  w. j  av  a  2s.  c  o m
        public Message createMessage(final Session session) throws JMSException {
            final ObjectMessage objectMessage = session.createObjectMessage(requestMessage);
            objectMessage.setJMSCorrelationID(requestMessage.getCorrelationUid());
            objectMessage.setJMSType(messageType);
            objectMessage.setStringProperty(Constants.ORGANISATION_IDENTIFICATION,
                    requestMessage.getOrganisationIdentification());
            objectMessage.setStringProperty(Constants.DEVICE_IDENTIFICATION,
                    requestMessage.getDeviceIdentification());

            return objectMessage;
        }

    });
}

From source file:com.alliander.osgp.signing.server.infra.messaging.SigningServerResponseMessageSender.java

private void sendMessage(final ResponseMessage responseMessage, final String messageType,
        final Destination replyToQueue) {
    this.responsesJmsTemplate.send(replyToQueue, new MessageCreator() {
        @Override/*  ww w  .j  a v a 2  s . c om*/
        public Message createMessage(final Session session) throws JMSException {
            final ObjectMessage objectMessage = session.createObjectMessage(responseMessage);
            objectMessage.setJMSCorrelationID(responseMessage.getCorrelationUid());
            objectMessage.setJMSType(messageType);
            objectMessage.setStringProperty(Constants.ORGANISATION_IDENTIFICATION,
                    responseMessage.getOrganisationIdentification());
            objectMessage.setStringProperty(Constants.DEVICE_IDENTIFICATION,
                    responseMessage.getDeviceIdentification());
            if (responseMessage.getOsgpException() != null) {
                objectMessage.setStringProperty(Constants.DESCRIPTION,
                        responseMessage.getOsgpException().getMessage());
            }
            return objectMessage;
        }
    });
}

From source file:com.alliander.osgp.adapter.ws.admin.infra.jms.AdminRequestMessageSender.java

/**
 * Method for sending a request message to the admin requests queue
 *
 * @param requestMessage/*  www .j ava2  s  .  co m*/
 *            The CommonRequestMessage request message to send.
 */
private void sendMessage(final AdminRequestMessage requestMessage) {
    LOGGER.info("Sending request message to admin requests queue");

    this.adminRequestsJmsTemplate.send(new MessageCreator() {

        @Override
        public Message createMessage(final Session session) throws JMSException {
            final ObjectMessage objectMessage = session.createObjectMessage(requestMessage.getRequest());
            objectMessage.setJMSCorrelationID(requestMessage.getCorrelationUid());
            objectMessage.setJMSType(requestMessage.getMessageType().toString());
            objectMessage.setStringProperty(Constants.ORGANISATION_IDENTIFICATION,
                    requestMessage.getOrganisationIdentification());
            objectMessage.setStringProperty(Constants.DEVICE_IDENTIFICATION,
                    requestMessage.getDeviceIdentification());
            return objectMessage;
        }

    });
}

From source file:com.alliander.osgp.adapter.ws.microgrids.infra.jms.MicrogridsRequestMessageSender.java

/**
 * Method for sending a request message to the microgrids requests queue
 *
 * @param requestMessage//from  ww w . jav a2 s .  c  o  m
 *            The MicrogridsRequestMessage request message to send.
 */
private void sendMessage(final MicrogridsRequestMessage requestMessage) {
    LOGGER.info("Sending message to the microgrids requests queue");

    this.microgridsRequestsJmsTemplate.send(new MessageCreator() {

        @Override
        public Message createMessage(final Session session) throws JMSException {
            final ObjectMessage objectMessage = session.createObjectMessage(requestMessage.getRequest());
            objectMessage.setJMSCorrelationID(requestMessage.getCorrelationUid());
            objectMessage.setJMSType(requestMessage.getMessageType().toString());
            objectMessage.setStringProperty(Constants.ORGANISATION_IDENTIFICATION,
                    requestMessage.getOrganisationIdentification());
            objectMessage.setStringProperty(Constants.DEVICE_IDENTIFICATION,
                    requestMessage.getDeviceIdentification());
            return objectMessage;
        }
    });
}

From source file:com.alliander.osgp.adapter.protocol.iec61850.infra.messaging.DeviceResponseMessageSender.java

private void sendMessage(final ProtocolResponseMessage responseMessage) {
    this.iec61850ResponsesJmsTemplate.send(new MessageCreator() {
        @Override/*w w w.j av  a  2  s . co  m*/
        public Message createMessage(final Session session) throws JMSException {
            final ObjectMessage objectMessage = session.createObjectMessage(responseMessage);
            objectMessage.setJMSCorrelationID(responseMessage.getCorrelationUid());
            objectMessage.setStringProperty(Constants.DOMAIN, responseMessage.getDomain());
            objectMessage.setStringProperty(Constants.DOMAIN_VERSION, responseMessage.getDomainVersion());
            objectMessage.setJMSType(responseMessage.getMessageType());
            objectMessage.setStringProperty(Constants.ORGANISATION_IDENTIFICATION,
                    responseMessage.getOrganisationIdentification());
            objectMessage.setStringProperty(Constants.DEVICE_IDENTIFICATION,
                    responseMessage.getDeviceIdentification());
            objectMessage.setStringProperty(Constants.RESULT, responseMessage.getResult().toString());
            if (responseMessage.getOsgpException() != null) {
                objectMessage.setStringProperty(Constants.DESCRIPTION,
                        responseMessage.getOsgpException().getMessage());
            }
            objectMessage.setBooleanProperty(Constants.IS_SCHEDULED, responseMessage.isScheduled());
            objectMessage.setIntProperty(Constants.RETRY_COUNT, responseMessage.getRetryCount());
            return objectMessage;
        }
    });
}

From source file:com.alliander.osgp.adapter.protocol.oslp.elster.infra.messaging.DeviceResponseMessageSender.java

private void sendMessage(final ProtocolResponseMessage responseMessage) {
    this.oslpResponsesJmsTemplate.send(new MessageCreator() {
        @Override/*from   w ww. j  av a2  s .  c  o m*/
        public Message createMessage(final Session session) throws JMSException {
            final ObjectMessage objectMessage = session.createObjectMessage(responseMessage);
            objectMessage.setJMSCorrelationID(responseMessage.getCorrelationUid());
            objectMessage.setStringProperty(Constants.DOMAIN, responseMessage.getDomain());
            objectMessage.setStringProperty(Constants.DOMAIN_VERSION, responseMessage.getDomainVersion());
            objectMessage.setJMSType(responseMessage.getMessageType());
            objectMessage.setStringProperty(Constants.ORGANISATION_IDENTIFICATION,
                    responseMessage.getOrganisationIdentification());
            objectMessage.setStringProperty(Constants.DEVICE_IDENTIFICATION,
                    responseMessage.getDeviceIdentification());
            objectMessage.setStringProperty(Constants.RESULT, responseMessage.getResult().toString());
            if (responseMessage.getOsgpException() != null) {
                objectMessage.setStringProperty(Constants.DESCRIPTION,
                        responseMessage.getOsgpException().getMessage());
            }
            objectMessage.setBooleanProperty(Constants.IS_SCHEDULED, responseMessage.isScheduled());
            objectMessage.setIntProperty(Constants.RETRY_COUNT, responseMessage.getRetryCount());
            return objectMessage;
        }
    });
}

From source file:org.osgp.adapter.protocol.dlms.infra.messaging.DeviceResponseMessageSender.java

private void sendMessage(final ProtocolResponseMessage responseMessage) {
    this.dlmsResponsesJmsTemplate.setPriority(responseMessage.getMessagePriority());
    this.dlmsResponsesJmsTemplate.send(new MessageCreator() {

        @Override//  www  .  j av  a 2  s.  c  o m
        public Message createMessage(final Session session) throws JMSException {
            final ObjectMessage objectMessage = session.createObjectMessage(responseMessage);
            objectMessage.setJMSCorrelationID(responseMessage.getCorrelationUid());
            objectMessage.setStringProperty(Constants.DOMAIN, responseMessage.getDomain());
            objectMessage.setStringProperty(Constants.DOMAIN_VERSION, responseMessage.getDomainVersion());
            objectMessage.setJMSType(responseMessage.getMessageType());
            objectMessage.setStringProperty(Constants.ORGANISATION_IDENTIFICATION,
                    responseMessage.getOrganisationIdentification());
            objectMessage.setStringProperty(Constants.DEVICE_IDENTIFICATION,
                    responseMessage.getDeviceIdentification());
            objectMessage.setStringProperty(Constants.RESULT, responseMessage.getResult().toString());
            if (responseMessage.getOsgpException() != null) {
                objectMessage.setStringProperty(Constants.DESCRIPTION,
                        responseMessage.getOsgpException().getMessage());
            }
            objectMessage.setBooleanProperty(Constants.IS_SCHEDULED, responseMessage.isScheduled());
            objectMessage.setIntProperty(Constants.RETRY_COUNT, responseMessage.getRetryCount());
            return objectMessage;
        }

    });
}

From source file:com.alliander.osgp.adapter.ws.smartmetering.infra.jms.SmartMeteringRequestMessageSender.java

/**
 * Method for sending a request message to the public lighting requests
 * queue// w  w w  . ja v  a2  s . co m
 *
 * @param requestMessage
 *            The SmartMeteringRequestMessage request message to send.
 */
private void sendMessage(final SmartMeteringRequestMessage requestMessage) {
    LOGGER.info("Sending message to the smart metering requests queue");

    this.smartMeteringRequestsJmsTemplate.setPriority(requestMessage.getMessagePriority());
    this.smartMeteringRequestsJmsTemplate.send(new MessageCreator() {

        @Override
        public Message createMessage(final Session session) throws JMSException {
            final ObjectMessage objectMessage = session.createObjectMessage(requestMessage.getRequest());
            objectMessage.setJMSCorrelationID(requestMessage.getCorrelationUid());
            objectMessage.setJMSType(requestMessage.getMessageType().toString());
            objectMessage.setStringProperty(Constants.ORGANISATION_IDENTIFICATION,
                    requestMessage.getOrganisationIdentification());
            objectMessage.setStringProperty(Constants.DEVICE_IDENTIFICATION,
                    requestMessage.getDeviceIdentification());
            if (requestMessage.getScheduleTime() != null) {
                objectMessage.setLongProperty(Constants.SCHEDULE_TIME, requestMessage.getScheduleTime());
            }
            return objectMessage;
        }
    });
}