Search in sources :

Example 1 with MessageCreator

use of org.springframework.jms.core.MessageCreator in project camel by apache.

the class JmsProducer method processInOut.

protected boolean processInOut(final Exchange exchange, final AsyncCallback callback) {
    final org.apache.camel.Message in = exchange.getIn();
    String destinationName = in.getHeader(JmsConstants.JMS_DESTINATION_NAME, String.class);
    // remove the header so it wont be propagated
    in.removeHeader(JmsConstants.JMS_DESTINATION_NAME);
    if (destinationName == null) {
        destinationName = endpoint.getDestinationName();
    }
    Destination destination = in.getHeader(JmsConstants.JMS_DESTINATION, Destination.class);
    // remove the header so it wont be propagated
    in.removeHeader(JmsConstants.JMS_DESTINATION);
    if (destination == null) {
        destination = endpoint.getDestination();
    }
    if (destination != null) {
        // prefer to use destination over destination name
        destinationName = null;
    }
    initReplyManager();
    // the request timeout can be overruled by a header otherwise the endpoint configured value is used
    final long timeout = exchange.getIn().getHeader(JmsConstants.JMS_REQUEST_TIMEOUT, endpoint.getRequestTimeout(), long.class);
    final JmsConfiguration configuration = endpoint.getConfiguration();
    // when using message id as correlation id, we need at first to use a provisional correlation id
    // which we then update to the real JMSMessageID when the message has been sent
    // this is done with the help of the MessageSentCallback
    final boolean msgIdAsCorrId = configuration.isUseMessageIDAsCorrelationID();
    final String provisionalCorrelationId = msgIdAsCorrId ? getUuidGenerator().generateUuid() : null;
    MessageSentCallback messageSentCallback = null;
    if (msgIdAsCorrId) {
        messageSentCallback = new UseMessageIdAsCorrelationIdMessageSentCallback(replyManager, provisionalCorrelationId, timeout);
    }
    final String correlationProperty = configuration.getCorrelationProperty();
    final String correlationPropertyToUse = ofNullable(correlationProperty).orElse("JMSCorrelationID");
    final String originalCorrelationId = in.getHeader(correlationPropertyToUse, String.class);
    boolean generateFreshCorrId = (ObjectHelper.isEmpty(originalCorrelationId) && !msgIdAsCorrId) || (originalCorrelationId != null && originalCorrelationId.startsWith(GENERATED_CORRELATION_ID_PREFIX));
    if (generateFreshCorrId) {
        // we append the 'Camel-' prefix to know it was generated by us
        in.setHeader(correlationPropertyToUse, GENERATED_CORRELATION_ID_PREFIX + getUuidGenerator().generateUuid());
    }
    MessageCreator messageCreator = new MessageCreator() {

        public Message createMessage(Session session) throws JMSException {
            Message answer = endpoint.getBinding().makeJmsMessage(exchange, in, session, null);
            Destination replyTo = null;
            String replyToOverride = configuration.getReplyToOverride();
            if (replyToOverride != null) {
                replyTo = resolveOrCreateDestination(replyToOverride, session);
            } else {
                // get the reply to destination to be used from the reply manager
                replyTo = replyManager.getReplyTo();
            }
            if (replyTo == null) {
                throw new RuntimeExchangeException("Failed to resolve replyTo destination", exchange);
            }
            JmsMessageHelper.setJMSReplyTo(answer, replyTo);
            replyManager.setReplyToSelectorHeader(in, answer);
            String correlationId = determineCorrelationId(answer, provisionalCorrelationId);
            replyManager.registerReply(replyManager, exchange, callback, originalCorrelationId, correlationId, timeout);
            if (correlationProperty != null) {
                replyManager.setCorrelationProperty(correlationProperty);
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("Using {}: {}, JMSReplyTo destination: {}, with request timeout: {} ms.", new Object[] { correlationPropertyToUse, correlationId, replyTo, timeout });
            }
            LOG.trace("Created javax.jms.Message: {}", answer);
            return answer;
        }
    };
    doSend(true, destinationName, destination, messageCreator, messageSentCallback);
    // continue routing asynchronously (reply will be processed async when its received)
    return false;
}
Also used : Destination(javax.jms.Destination) Message(javax.jms.Message) UseMessageIdAsCorrelationIdMessageSentCallback(org.apache.camel.component.jms.reply.UseMessageIdAsCorrelationIdMessageSentCallback) UseMessageIdAsCorrelationIdMessageSentCallback(org.apache.camel.component.jms.reply.UseMessageIdAsCorrelationIdMessageSentCallback) MessageCreator(org.springframework.jms.core.MessageCreator) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException) Session(javax.jms.Session)

Example 2 with MessageCreator

use of org.springframework.jms.core.MessageCreator in project camel by apache.

the class JmsProducer method processInOnly.

protected boolean processInOnly(final Exchange exchange, final AsyncCallback callback) {
    final org.apache.camel.Message in = exchange.getIn();
    String destinationName = in.getHeader(JmsConstants.JMS_DESTINATION_NAME, String.class);
    if (destinationName != null) {
        // remove the header so it wont be propagated
        in.removeHeader(JmsConstants.JMS_DESTINATION_NAME);
    }
    if (destinationName == null) {
        destinationName = endpoint.getDestinationName();
    }
    Destination destination = in.getHeader(JmsConstants.JMS_DESTINATION, Destination.class);
    if (destination != null) {
        // remove the header so it wont be propagated
        in.removeHeader(JmsConstants.JMS_DESTINATION);
    }
    if (destination == null) {
        destination = endpoint.getDestination();
    }
    if (destination != null) {
        // prefer to use destination over destination name
        destinationName = null;
    }
    final String to = destinationName != null ? destinationName : "" + destination;
    MessageSentCallback messageSentCallback = getEndpoint().getConfiguration().isIncludeSentJMSMessageID() ? new InOnlyMessageSentCallback(exchange) : null;
    MessageCreator messageCreator = new MessageCreator() {

        public Message createMessage(Session session) throws JMSException {
            Message answer = endpoint.getBinding().makeJmsMessage(exchange, in, session, null);
            // when in InOnly mode the JMSReplyTo is a bit complicated
            // we only want to set the JMSReplyTo on the answer if
            // there is a JMSReplyTo from the header/endpoint and
            // we have been told to preserveMessageQos
            Object jmsReplyTo = JmsMessageHelper.getJMSReplyTo(answer);
            if (endpoint.isDisableReplyTo()) {
                // honor disable reply to configuration
                LOG.trace("ReplyTo is disabled on endpoint: {}", endpoint);
                JmsMessageHelper.setJMSReplyTo(answer, null);
            } else {
                // if the binding did not create the reply to then we have to try to create it here
                if (jmsReplyTo == null) {
                    // prefer reply to from header over endpoint configured
                    jmsReplyTo = exchange.getIn().getHeader("JMSReplyTo", String.class);
                    if (jmsReplyTo == null) {
                        jmsReplyTo = endpoint.getReplyTo();
                    }
                }
            }
            // unless we use preserveMessageQos=true to tell that we still want to use JMSReplyTo
            if (jmsReplyTo != null && !(endpoint.isPreserveMessageQos() || endpoint.isExplicitQosEnabled())) {
                // this behavior is also documented at the camel website
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Disabling JMSReplyTo: {} for destination: {}. Use preserveMessageQos=true to force Camel to keep the JMSReplyTo on endpoint: {}", new Object[] { jmsReplyTo, to, endpoint });
                }
                jmsReplyTo = null;
            }
            // and if needed create the destination using the session if needed to
            if (jmsReplyTo != null && jmsReplyTo instanceof String) {
                String replyTo = (String) jmsReplyTo;
                // we need to null it as we use the String to resolve it as a Destination instance
                jmsReplyTo = resolveOrCreateDestination(replyTo, session);
            }
            // set the JMSReplyTo on the answer if we are to use it
            Destination replyTo = null;
            String replyToOverride = endpoint.getConfiguration().getReplyToOverride();
            if (replyToOverride != null) {
                replyTo = resolveOrCreateDestination(replyToOverride, session);
            } else if (jmsReplyTo instanceof Destination) {
                replyTo = (Destination) jmsReplyTo;
            }
            if (replyTo != null) {
                LOG.debug("Using JMSReplyTo destination: {}", replyTo);
                JmsMessageHelper.setJMSReplyTo(answer, replyTo);
            } else {
                // do not use JMSReplyTo
                log.trace("Not using JMSReplyTo");
                JmsMessageHelper.setJMSReplyTo(answer, null);
            }
            LOG.trace("Created javax.jms.Message: {}", answer);
            return answer;
        }
    };
    doSend(false, destinationName, destination, messageCreator, messageSentCallback);
    // after sending then set the OUT message id to the JMSMessageID so its identical
    setMessageId(exchange);
    // we are synchronous so return true
    callback.done(true);
    return true;
}
Also used : Destination(javax.jms.Destination) Message(javax.jms.Message) UseMessageIdAsCorrelationIdMessageSentCallback(org.apache.camel.component.jms.reply.UseMessageIdAsCorrelationIdMessageSentCallback) MessageCreator(org.springframework.jms.core.MessageCreator) Session(javax.jms.Session)

Example 3 with MessageCreator

use of org.springframework.jms.core.MessageCreator in project camel by apache.

the class ConsumeJmsObjectMessageTest method testConsumeObjectMessage.

@Test
public void testConsumeObjectMessage() throws Exception {
    endpoint.expectedMessageCount(1);
    jmsTemplate.setPubSubDomain(false);
    jmsTemplate.send("test.object", new MessageCreator() {

        public Message createMessage(Session session) throws JMSException {
            ObjectMessage msg = session.createObjectMessage();
            MyUser user = new MyUser();
            user.setName("Claus");
            msg.setObject(user);
            return msg;
        }
    });
    endpoint.assertIsSatisfied();
    assertCorrectObjectReceived();
}
Also used : ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) ObjectMessage(javax.jms.ObjectMessage) JMSException(javax.jms.JMSException) MessageCreator(org.springframework.jms.core.MessageCreator) Session(javax.jms.Session) Test(org.junit.Test)

Example 4 with MessageCreator

use of org.springframework.jms.core.MessageCreator in project spring-boot by spring-projects.

the class ArtemisAutoConfigurationTests method embeddedWithPersistentMode.

@Test
public void embeddedWithPersistentMode() throws IOException, JMSException {
    File dataFolder = this.folder.newFolder();
    // Start the server and post a message to some queue
    load(EmptyConfiguration.class, "spring.artemis.embedded.queues=TestQueue", "spring.artemis.embedded.persistent:true", "spring.artemis.embedded.dataDirectory:" + dataFolder.getAbsolutePath());
    final String msgId = UUID.randomUUID().toString();
    JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
    jmsTemplate.send("TestQueue", new MessageCreator() {

        @Override
        public Message createMessage(Session session) throws JMSException {
            return session.createTextMessage(msgId);
        }
    });
    // Shutdown the broker
    this.context.close();
    // Start the server again and check if our message is still here
    load(EmptyConfiguration.class, "spring.artemis.embedded.queues=TestQueue", "spring.artemis.embedded.persistent:true", "spring.artemis.embedded.dataDirectory:" + dataFolder.getAbsolutePath());
    JmsTemplate jmsTemplate2 = this.context.getBean(JmsTemplate.class);
    jmsTemplate2.setReceiveTimeout(1000L);
    Message message = jmsTemplate2.receive("TestQueue");
    assertThat(message).isNotNull();
    assertThat(((TextMessage) message).getText()).isEqualTo(msgId);
}
Also used : Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) JmsTemplate(org.springframework.jms.core.JmsTemplate) JMSException(javax.jms.JMSException) File(java.io.File) MessageCreator(org.springframework.jms.core.MessageCreator) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) Test(org.junit.Test)

Example 5 with MessageCreator

use of org.springframework.jms.core.MessageCreator in project Protocol-Adapter-IEC61850 by OSGP.

the class Iec61850LogItemRequestMessageSender method send.

public void send(final Iec61850LogItemRequestMessage iec61850LogItemRequestMessage) {
    LOGGER.debug("Sending Iec61850LogItemRequestMessage");
    this.iec61850LogItemRequestsJmsTemplate.send(new MessageCreator() {

        @Override
        public Message createMessage(final Session session) throws JMSException {
            final ObjectMessage objectMessage = session.createObjectMessage();
            objectMessage.setJMSType(Constants.IEC61850_LOG_ITEM_REQUEST);
            objectMessage.setStringProperty(Constants.IS_INCOMING, iec61850LogItemRequestMessage.isIncoming().toString());
            objectMessage.setStringProperty(Constants.ENCODED_MESSAGE, iec61850LogItemRequestMessage.getEncodedMessage());
            objectMessage.setStringProperty(Constants.DECODED_MESSAGE, iec61850LogItemRequestMessage.getDecodedMessage());
            objectMessage.setStringProperty(Constants.DEVICE_IDENTIFICATION, iec61850LogItemRequestMessage.getDeviceIdentification());
            objectMessage.setStringProperty(Constants.ORGANISATION_IDENTIFICATION, iec61850LogItemRequestMessage.getOrganisationIdentification());
            objectMessage.setStringProperty(Constants.IS_VALID, iec61850LogItemRequestMessage.isValid().toString());
            objectMessage.setIntProperty(Constants.PAYLOAD_MESSAGE_SERIALIZED_SIZE, iec61850LogItemRequestMessage.getPayloadMessageSerializedSize());
            return objectMessage;
        }
    });
}
Also used : ObjectMessage(javax.jms.ObjectMessage) Message(javax.jms.Message) ObjectMessage(javax.jms.ObjectMessage) JMSException(javax.jms.JMSException) MessageCreator(org.springframework.jms.core.MessageCreator) Session(javax.jms.Session)

Aggregations

Message (javax.jms.Message)12 Session (javax.jms.Session)12 MessageCreator (org.springframework.jms.core.MessageCreator)12 JMSException (javax.jms.JMSException)10 Test (org.junit.Test)7 Destination (javax.jms.Destination)4 ObjectMessage (javax.jms.ObjectMessage)3 TextMessage (javax.jms.TextMessage)3 JmsTemplate (org.springframework.jms.core.JmsTemplate)3 UseMessageIdAsCorrelationIdMessageSentCallback (org.apache.camel.component.jms.reply.UseMessageIdAsCorrelationIdMessageSentCallback)2 RequestMessage (com.alliander.osgp.shared.infra.jms.RequestMessage)1 File (java.io.File)1 ExecutorService (java.util.concurrent.ExecutorService)1 BytesMessage (javax.jms.BytesMessage)1 Connection (javax.jms.Connection)1 ConnectionFactory (javax.jms.ConnectionFactory)1 MapMessage (javax.jms.MapMessage)1 MessageProducer (javax.jms.MessageProducer)1 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)1 RuntimeExchangeException (org.apache.camel.RuntimeExchangeException)1