org.apache.camel.component.sjms.producer.InOutProducer Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.camel.component.sjms.producer;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Exchanger;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import org.apache.camel.AsyncCallback;
import org.apache.camel.CamelException;
import org.apache.camel.Exchange;
import org.apache.camel.component.sjms.MessageConsumerResources;
import org.apache.camel.component.sjms.MessageProducerResources;
import org.apache.camel.component.sjms.SjmsEndpoint;
import org.apache.camel.component.sjms.SjmsMessage;
import org.apache.camel.component.sjms.SjmsProducer;
import org.apache.camel.component.sjms.jms.ConnectionResource;
import org.apache.camel.component.sjms.jms.DestinationNameParser;
import org.apache.camel.component.sjms.jms.JmsConstants;
import org.apache.camel.component.sjms.jms.JmsMessageHelper;
import org.apache.camel.spi.UuidGenerator;
import org.apache.camel.util.ObjectHelper;
import org.apache.commons.pool.BasePoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
/**
* A Camel Producer that provides the InOut Exchange pattern.
*/
public class InOutProducer extends SjmsProducer {
private static final Map> EXCHANGERS = new ConcurrentHashMap<>();
private static final String GENERATED_CORRELATION_ID_PREFIX = "Camel-";
private UuidGenerator uuidGenerator;
private GenericObjectPool consumers;
public InOutProducer(final SjmsEndpoint endpoint) {
super(endpoint);
}
public UuidGenerator getUuidGenerator() {
return uuidGenerator;
}
public void setUuidGenerator(UuidGenerator uuidGenerator) {
this.uuidGenerator = uuidGenerator;
}
/**
* A pool of {@link MessageConsumerResources} objects that are the reply consumers.
*/
protected class MessageConsumerResourcesFactory extends BasePoolableObjectFactory {
@Override
public MessageConsumerResources makeObject() throws Exception {
MessageConsumerResources answer;
ConnectionResource connectionResource = getOrCreateConnectionResource();
Connection conn = connectionResource.borrowConnection();
try {
Session session;
if (isEndpointTransacted()) {
session = conn.createSession(true, Session.SESSION_TRANSACTED);
} else {
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
}
Destination replyToDestination;
boolean isReplyToTopic = false;
if (ObjectHelper.isEmpty(getNamedReplyTo())) {
isReplyToTopic = isTopic();
replyToDestination = getEndpoint().getDestinationCreationStrategy().createTemporaryDestination(session, isReplyToTopic);
} else {
DestinationNameParser parser = new DestinationNameParser();
isReplyToTopic = parser.isNamedReplyToTopic(getNamedReplyTo(), isTopic());
replyToDestination = getEndpoint().getDestinationCreationStrategy().createDestination(session, getNamedReplyTo(), isReplyToTopic);
}
MessageConsumer messageConsumer = getEndpoint().getJmsObjectFactory().createMessageConsumer(session, replyToDestination, null, isReplyToTopic, null, true, false, false);
messageConsumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(final Message message) {
log.debug("Message Received in the Consumer Pool");
log.debug(" Message : {}", message);
try {
Exchanger
© 2015 - 2025 Weber Informatics LLC | Privacy Policy