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

org.ow2.jasmine.mule.providers.jms.JasmineJmsMessageDispatcher Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2012 Bull S.A.S.
 *
 * 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 org.ow2.jasmine.mule.providers.jms;


import org.mule.impl.MuleMessage;
import org.mule.providers.AbstractMessageDispatcher;
import org.mule.providers.jms.JmsConnector;
import org.mule.providers.jms.JmsConstants;
import org.mule.providers.jms.JmsSupport;
import org.mule.providers.jms.filters.JmsSelectorFilter;
import org.mule.providers.jms.i18n.JmsMessages;
import org.mule.umo.UMOEvent;
import org.mule.umo.UMOMessage;
import org.mule.umo.endpoint.UMOEndpointURI;
import org.mule.umo.endpoint.UMOImmutableEndpoint;
import org.mule.umo.provider.DispatchException;
import org.mule.umo.provider.UMOConnector;
import org.mule.util.ClassUtils;
import org.mule.util.NumberUtils;
import org.mule.util.StringUtils;

import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.commons.lang.BooleanUtils;

/**
 * JasmineJmsMessageDispatcher is just JmsMessageDispatcher with some optimizations.
 * 

* JmsMessageDispatcher is responsible for dispatching messages to JMS * destinations. All JMS semantics apply and settings such as replyTo and QoS * properties are read from the event properties or defaults are used (according to * the JMS specification) */ public class JasmineJmsMessageDispatcher extends AbstractMessageDispatcher { private JmsConnector connector; private JmsSupport jms; public JasmineJmsMessageDispatcher(UMOImmutableEndpoint endpoint) { super(endpoint); this.connector = (JmsConnector) endpoint.getConnector(); this.jms = connector.getJmsSupport(); } protected void doDispatch(UMOEvent event) throws Exception { logger.debug(""); dispatchMessage(event); } protected void doConnect() throws Exception { logger.debug(""); } protected void doDisconnect() throws Exception { logger.debug(""); } private UMOMessage dispatchMessage(UMOEvent event) throws Exception { Session session = null; MessageProducer producer = null; boolean transacted = false; if (logger.isDebugEnabled()) { logger.debug("dispatching on endpoint: " + event.getEndpoint().getEndpointURI() + ". Event id is: " + event.getId()); } try { session = connector.getSessionFromTransaction(); if (session != null) { transacted = true; } else { session = connector.getSession(event.getEndpoint()); if (event.getEndpoint().getTransactionConfig().isTransacted()) { transacted = true; } } UMOEndpointURI endpointUri = event.getEndpoint().getEndpointURI(); boolean topic = connector.getTopicResolver().isTopic(event.getEndpoint(), true); Destination dest = jms.createDestination(session, endpointUri.getAddress(), topic); producer = jms.createProducer(session, dest, topic); Object message = event.getTransformedMessage(); if (!(message instanceof Message)) { throw new DispatchException( JmsMessages.checkTransformer("JMS message", message.getClass(), connector.getName()), event.getMessage(), event.getEndpoint()); } Message msg = (Message) message; if (event.getMessage().getCorrelationId() != null) { msg.setJMSCorrelationID(event.getMessage().getCorrelationId()); } UMOMessage eventMsg = event.getMessage(); // QoS support String ttlString = (String) eventMsg.removeProperty(JmsConstants.TIME_TO_LIVE_PROPERTY); String priorityString = (String) eventMsg.removeProperty(JmsConstants.PRIORITY_PROPERTY); String persistentDeliveryString = (String) eventMsg.removeProperty(JmsConstants.PERSISTENT_DELIVERY_PROPERTY); long ttl = StringUtils.isNotBlank(ttlString) ? NumberUtils.toLong(ttlString) : Message.DEFAULT_TIME_TO_LIVE; int priority = StringUtils.isNotBlank(priorityString) ? NumberUtils.toInt(priorityString) : Message.DEFAULT_PRIORITY; boolean persistent = StringUtils.isNotBlank(persistentDeliveryString) ? BooleanUtils.toBoolean(persistentDeliveryString) : connector.isPersistentDelivery(); if (connector.isHonorQosHeaders()) { int priorityProp = eventMsg.getIntProperty(JmsConstants.JMS_PRIORITY, UMOConnector.INT_VALUE_NOT_SET); int deliveryModeProp = eventMsg.getIntProperty(JmsConstants.JMS_DELIVERY_MODE, UMOConnector.INT_VALUE_NOT_SET); if (priorityProp != UMOConnector.INT_VALUE_NOT_SET) { priority = priorityProp; } if (deliveryModeProp != UMOConnector.INT_VALUE_NOT_SET) { persistent = deliveryModeProp == DeliveryMode.PERSISTENT; } } if (logger.isDebugEnabled()) { logger.debug("Sending message of type " + ClassUtils.getSimpleName(msg.getClass())); } jms.send(producer, msg, persistent, priority, ttl, topic); return null; } finally { connector.closeQuietly(producer); // If the session is from the current transaction, it is up to the // transaction to close it. if (session != null && !transacted) { connector.closeQuietly(session); } } } protected UMOMessage doSend(UMOEvent event) throws Exception { logger.debug(""); UMOMessage message = dispatchMessage(event); return message; } /** * Make a specific request to the underlying transport * * @param timeout the maximum time the operation should block before returning. * The call should return immediately if there is data available. If * no data becomes available before the timeout elapses, null will be * returned * @return the result of the request wrapped in a UMOMessage object. Null will be * returned if no data was available * @throws Exception if the call to the underlying protocol causes an exception */ protected UMOMessage doReceive(long timeout) throws Exception { Session session = null; MessageConsumer consumer = null; logger.debug(""); try { final boolean topic = connector.getTopicResolver().isTopic(endpoint); session = connector.getSession(false, topic); Destination dest = jms.createDestination(session, endpoint.getEndpointURI().getAddress(), topic); // Extract jms selector String selector = null; if (endpoint.getFilter() != null && endpoint.getFilter() instanceof JmsSelectorFilter) { selector = ((JmsSelectorFilter) endpoint.getFilter()).getExpression(); } else if (endpoint.getProperties() != null) { // still allow the selector to be set as a property on the endpoint // to be backward compatible selector = (String) endpoint.getProperties().get(JmsConstants.JMS_SELECTOR_PROPERTY); } String tempDurable = (String) endpoint.getProperties().get(JmsConstants.DURABLE_PROPERTY); boolean durable = connector.isDurable(); if (tempDurable != null) { durable = Boolean.valueOf(tempDurable).booleanValue(); } // Get the durable subscriber name if there is one String durableName = (String) endpoint.getProperties().get(JmsConstants.DURABLE_NAME_PROPERTY); if (durableName == null && durable && topic) { durableName = "mule." + connector.getName() + "." + endpoint.getEndpointURI().getAddress(); if (logger.isDebugEnabled()) { logger.debug("Jms Connector for this receiver is durable but no durable name has been specified. Defaulting to: " + durableName); } } // Create consumer consumer = jms.createConsumer(session, dest, selector, connector.isNoLocal(), durableName, topic); try { Message message; if (timeout == RECEIVE_NO_WAIT) { message = consumer.receiveNoWait(); } else if (timeout == RECEIVE_WAIT_INDEFINITELY) { message = consumer.receive(); } else { message = consumer.receive(timeout); } if (message == null) { return null; } message = connector.preProcessMessage(message, session); return new MuleMessage(connector.getMessageAdapter(message)); } catch (Exception e) { connector.handleException(e); return null; } } finally { connector.closeQuietly(consumer); connector.closeQuietly(session); } } protected void doDispose() { logger.debug(""); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy