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.spring.integration;
018
019 import org.apache.camel.ExchangePattern;
020 import org.apache.camel.Processor;
021 import org.apache.camel.RuntimeCamelException;
022 import org.apache.camel.impl.DefaultConsumer;
023 import org.apache.camel.impl.ScheduledPollConsumer;
024 import org.apache.camel.spring.SpringCamelContext;
025 import org.apache.camel.util.ObjectHelper;
026 import org.apache.camel.util.ServiceHelper;
027 import org.springframework.integration.channel.AbstractPollableChannel;
028 import org.springframework.integration.channel.BeanFactoryChannelResolver;
029 import org.springframework.integration.channel.ChannelResolver;
030 import org.springframework.integration.channel.DirectChannel;
031 import org.springframework.integration.core.Message;
032 import org.springframework.integration.core.MessageChannel;
033 import org.springframework.integration.message.MessageHandler;
034
035 /**
036 * A consumer of exchanges for the Spring Integration
037 * Please specify the inputChannel in the endpoint url for this consumer.
038 * If the message pattern is inOut, the outputChannel property
039 * should be set for the outgoing message.
040 *
041 * @version $Revision: 711533 $
042 */
043 public class SpringIntegrationConsumer extends DefaultConsumer<SpringIntegrationExchange> implements MessageHandler {
044 private SpringCamelContext context;
045 private DirectChannel inputChannel;
046 private MessageChannel outputChannel;
047 private String inputChannelName;
048 private ChannelResolver channelResolver;
049 private SpringIntegrationEndpoint endpoint;
050
051 public SpringIntegrationConsumer(SpringIntegrationEndpoint endpoint, Processor processor) {
052 super(endpoint, processor);
053 this.endpoint = endpoint;
054 context = (SpringCamelContext) endpoint.getCamelContext();
055 if (context != null && endpoint.getMessageChannel() == null) {
056 channelResolver = new BeanFactoryChannelResolver(context.getApplicationContext());
057 inputChannelName = endpoint.getDefaultChannel();
058 if (ObjectHelper.isNullOrBlank(inputChannelName)) {
059 inputChannelName = endpoint.getInputChannel();
060 }
061 if (!ObjectHelper.isNullOrBlank(inputChannelName)) {
062 inputChannel = (DirectChannel) channelResolver.resolveChannelName(inputChannelName);
063 ObjectHelper.notNull(inputChannel, "The inputChannel with the name [" + inputChannelName + "]");
064 } else {
065 throw new RuntimeCamelException("Can't find the right inputChannelName, please check your configuration.");
066 }
067 } else {
068 if (endpoint.getMessageChannel() != null) {
069 inputChannel = (DirectChannel)endpoint.getMessageChannel();
070 } else {
071 throw new RuntimeCamelException("Can't find the right message channel, please check your configuration.");
072 }
073 }
074 if (endpoint.isInOut()) {
075 endpoint.setExchangePattern(ExchangePattern.InOut);
076 }
077
078 }
079
080 protected void doStop() throws Exception {
081 inputChannel.unsubscribe(this);
082 super.doStop();
083 }
084
085 protected void doStart() throws Exception {
086 super.doStart();
087 inputChannel.subscribe(this);
088 }
089
090 public void handleMessage(org.springframework.integration.core.Message<?> siInMessage) {
091 SpringIntegrationExchange exchange = getEndpoint().createExchange();
092 exchange.setIn(new SpringIntegrationMessage(siInMessage));
093 try {
094 getProcessor().process(exchange);
095 } catch (Exception e) {
096 //TODO need to find a way to deal with this exception
097 throw ObjectHelper.wrapRuntimeCamelException(e);
098 }
099 if (endpoint.isInOut()) {
100 // get the output channel from message header
101 Object returnAddress = siInMessage.getHeaders().getReplyChannel();
102 MessageChannel reply = null;
103
104 if (returnAddress != null) {
105 if (returnAddress instanceof String) {
106 reply = (MessageChannel)context.getApplicationContext().getBean((String)returnAddress);
107 } else if (returnAddress instanceof MessageChannel) {
108 reply = (MessageChannel) returnAddress;
109 }
110 } else {
111 if (outputChannel != null) {
112 // using the outputChannel
113 reply = outputChannel;
114 } else {
115 if (ObjectHelper.isNullOrBlank(endpoint.getOutputChannel())) {
116 outputChannel = (MessageChannel) channelResolver.resolveChannelName(endpoint.getOutputChannel());
117 ObjectHelper.notNull(inputChannel, "The outputChannel with the name [" + endpoint.getOutputChannel() + "]");
118 reply = outputChannel;
119 } else {
120 throw new RuntimeCamelException("Can't find the right outputChannelName");
121 }
122 }
123 }
124 // put the message back the outputChannel if we need
125 org.springframework.integration.core.Message siOutMessage =
126 SpringIntegrationBinding.storeToSpringIntegrationMessage(exchange.getOut());
127 reply.send(siOutMessage);
128 }
129 }
130
131 }