001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.component.jms.requestor;
018
019 import java.util.concurrent.ExecutionException;
020 import java.util.concurrent.TimeUnit;
021 import java.util.concurrent.TimeoutException;
022
023 import javax.jms.Message;
024
025 import org.apache.camel.component.jms.requestor.DeferredRequestReplyMap.DeferredMessageSentCallback;
026 import org.apache.camel.component.jms.requestor.PersistentReplyToRequestor.MessageSelectorComposer;
027
028 public class PersistentReplyToFutureHandler extends FutureHandler {
029
030 protected PersistentReplyToRequestor requestor;
031 protected DeferredMessageSentCallback callback;
032 protected String correlationID;
033
034 public PersistentReplyToFutureHandler(PersistentReplyToRequestor requestor,
035 String correlationID) {
036 super();
037 this.requestor = requestor;
038 this.correlationID = correlationID;
039 }
040
041 public PersistentReplyToFutureHandler(PersistentReplyToRequestor requestor,
042 DeferredMessageSentCallback callback) {
043 super();
044 this.requestor = requestor;
045 this.callback = callback;
046 }
047
048 @Override
049 public Message get() throws InterruptedException, ExecutionException {
050 Message result = null;
051 try {
052 updateSelector();
053 result = super.get();
054 } finally {
055 revertSelector();
056 }
057 return result;
058 }
059
060 @Override
061 public Message get(long timeout, TimeUnit unit) throws InterruptedException,
062 ExecutionException,
063 TimeoutException {
064 Message result = null;
065 try {
066 updateSelector();
067 result = super.get(timeout, unit);
068 } finally {
069 revertSelector();
070 }
071 return result;
072 }
073
074 protected void updateSelector() throws ExecutionException {
075 try {
076 MessageSelectorComposer composer = (MessageSelectorComposer)requestor.getListenerContainer();
077 composer.addCorrelationID((correlationID != null) ? correlationID : callback.getMessage().getJMSMessageID());
078 } catch (Exception e) {
079 throw new ExecutionException(e);
080 }
081 }
082
083 protected void revertSelector() throws ExecutionException {
084 try {
085 MessageSelectorComposer composer = (MessageSelectorComposer)requestor.getListenerContainer();
086 composer.removeCorrelationID((correlationID != null) ? correlationID : callback.getMessage().getJMSMessageID());
087 } catch (Exception e) {
088 throw new ExecutionException(e);
089 }
090 }
091 }