All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.consol.citrus.jms.endpoint.JmsProducer Maven / Gradle / Ivy

/*
 * Copyright 2006-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.consol.citrus.jms.endpoint;

import com.consol.citrus.context.TestContext;
import com.consol.citrus.exceptions.CitrusRuntimeException;
import com.consol.citrus.message.Message;
import com.consol.citrus.messaging.Producer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.jms.Destination;

/**
 * @author Christoph Deppisch
 * @since 1.4
 */
public class JmsProducer implements Producer {

    /** Logger */
    private static Logger log = LoggerFactory.getLogger(JmsProducer.class);

    /** The producer name. */
    private final String name;

    /** Endpoint configuration */
    private final JmsEndpointConfiguration endpointConfiguration;

    /**
     * Default constructor using endpoint configuration.
     * @param name
     * @param endpointConfiguration
     */
    public JmsProducer(String name, JmsEndpointConfiguration endpointConfiguration) {
        this.name = name;
        this.endpointConfiguration = endpointConfiguration;
    }

    @Override
    public void send(final Message message, final TestContext context) {
        Assert.notNull(message, "Message is empty - unable to send empty message");

        if (endpointConfiguration.getDestination() != null) {
            send(message, endpointConfiguration.getDestination(), context);
        } else if (StringUtils.hasText(endpointConfiguration.getDestinationName())) {
            if (endpointConfiguration.getDestinationNameResolver() != null) {
                send(message, context.replaceDynamicContentInString(endpointConfiguration.getDestinationNameResolver().resolveEndpointUri(message, endpointConfiguration.getDestinationName())), context);
            } else {
                send(message, context.replaceDynamicContentInString(endpointConfiguration.getDestinationName()), context);
            }
        } else if (endpointConfiguration.getJmsTemplate().getDefaultDestination() != null) {
            send(message, endpointConfiguration.getJmsTemplate().getDefaultDestination(), context);
        } else if (StringUtils.hasText(endpointConfiguration.getJmsTemplate().getDefaultDestinationName())) {
            send(message, context.replaceDynamicContentInString(endpointConfiguration.getJmsTemplate().getDefaultDestinationName()), context);
        } else {
            throw new CitrusRuntimeException("Unable to send message - JMS destination not set");
        }

        context.onOutboundMessage(message);
    }

    /**
     * Send message using destination name.
     * @param message
     * @param destinationName
     * @param context
     */
    private void send(Message message, String destinationName, TestContext context) {
        if (log.isDebugEnabled()) {
            log.debug("Sending JMS message to destination: '" + destinationName + "'");
        }

        endpointConfiguration.getJmsTemplate().send(destinationName, session -> {
            javax.jms.Message jmsMessage = endpointConfiguration.getMessageConverter().createJmsMessage(message, session, endpointConfiguration, context);
            endpointConfiguration.getMessageConverter().convertOutbound(jmsMessage, message, endpointConfiguration, context);
            return jmsMessage;
        });

        log.info("Message was sent to JMS destination: '" + destinationName + "'");
    }

    /**
     * Send message using destination.
     * @param message
     * @param destination
     * @param context
     */
    private void send(Message message, Destination destination, TestContext context) {
        if (log.isDebugEnabled()) {
            log.debug("Sending JMS message to destination: '" + endpointConfiguration.getDestinationName(destination) + "'");
        }

        endpointConfiguration.getJmsTemplate().send(destination, session -> {
            javax.jms.Message jmsMessage = endpointConfiguration.getMessageConverter().createJmsMessage(message, session, endpointConfiguration, context);
            endpointConfiguration.getMessageConverter().convertOutbound(jmsMessage, message, endpointConfiguration, context);
            return jmsMessage;
        });

        log.info("Message was sent to JMS destination: '" + endpointConfiguration.getDestinationName(destination) + "'");
    }

    @Override
    public String getName() {
        return name;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy