From 3ae8d545111cd6d1421d1bdb346d83fa58219149 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Mon, 10 Dec 2007 08:33:39 +0000 Subject: [PATCH] Implemented simple JMS adapters. This version of the inbound adapter is poll-based. --- .../jms/JmsInboundChannelAdapter.java | 70 ++++++++++++++++++ .../jms/JmsOutboundChannelAdapter.java | 71 +++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 spring-eai-core/src/main/java/org/springframework/integration/endpoint/jms/JmsInboundChannelAdapter.java create mode 100644 spring-eai-core/src/main/java/org/springframework/integration/endpoint/jms/JmsOutboundChannelAdapter.java diff --git a/spring-eai-core/src/main/java/org/springframework/integration/endpoint/jms/JmsInboundChannelAdapter.java b/spring-eai-core/src/main/java/org/springframework/integration/endpoint/jms/JmsInboundChannelAdapter.java new file mode 100644 index 0000000000..9d53570e94 --- /dev/null +++ b/spring-eai-core/src/main/java/org/springframework/integration/endpoint/jms/JmsInboundChannelAdapter.java @@ -0,0 +1,70 @@ +/* + * Copyright 2002-2007 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 org.springframework.integration.endpoint.jms; + +import javax.jms.ConnectionFactory; +import javax.jms.Destination; + +import org.springframework.integration.MessagingConfigurationException; +import org.springframework.integration.endpoint.AbstractInboundChannelAdapter; +import org.springframework.jms.core.JmsTemplate; + +/** + * A simple channel adapter for receiving JMS Messages with a polling listener. + * + * @author Mark Fisher + */ +public class JmsInboundChannelAdapter extends AbstractInboundChannelAdapter { + + private ConnectionFactory connectionFactory; + + private Destination destination; + + private JmsTemplate jmsTemplate; + + + public void setConnectionFactory(ConnectionFactory connectionFactory) { + this.connectionFactory = connectionFactory; + } + + public void setDestination(Destination destination) { + this.destination = destination; + } + + public void setJmsTemplate(JmsTemplate jmsTemplate) { + this.jmsTemplate = jmsTemplate; + } + + @Override + public void initialize() { + if (this.jmsTemplate == null) { + if (this.connectionFactory == null || this.destination == null) { + throw new MessagingConfigurationException("Either a 'jmsTemplate' or " + + "*both* 'connectionFactory' and 'destination' are required."); + } + this.jmsTemplate = new JmsTemplate(); + this.jmsTemplate.setConnectionFactory(this.connectionFactory); + this.jmsTemplate.setDefaultDestination(this.destination); + } + } + + @Override + protected Object doReceiveObject() { + return this.jmsTemplate.receiveAndConvert(); + } + +} diff --git a/spring-eai-core/src/main/java/org/springframework/integration/endpoint/jms/JmsOutboundChannelAdapter.java b/spring-eai-core/src/main/java/org/springframework/integration/endpoint/jms/JmsOutboundChannelAdapter.java new file mode 100644 index 0000000000..a4a1d6f4dc --- /dev/null +++ b/spring-eai-core/src/main/java/org/springframework/integration/endpoint/jms/JmsOutboundChannelAdapter.java @@ -0,0 +1,71 @@ +/* + * Copyright 2002-2007 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 org.springframework.integration.endpoint.jms; + +import javax.jms.ConnectionFactory; +import javax.jms.Destination; + +import org.springframework.integration.MessagingConfigurationException; +import org.springframework.integration.endpoint.AbstractOutboundChannelAdapter; +import org.springframework.jms.core.JmsTemplate; + +/** + * A simple channel adapter for sending JMS Messages. + * + * @author Mark Fisher + */ +public class JmsOutboundChannelAdapter extends AbstractOutboundChannelAdapter { + + private ConnectionFactory connectionFactory; + + private Destination destination; + + private JmsTemplate jmsTemplate; + + + public void setConnectionFactory(ConnectionFactory connectionFactory) { + this.connectionFactory = connectionFactory; + } + + public void setDestination(Destination destination) { + this.destination = destination; + } + + public void setJmsTemplate(JmsTemplate jmsTemplate) { + this.jmsTemplate = jmsTemplate; + } + + @Override + public void initialize() { + if (this.jmsTemplate == null) { + if (this.connectionFactory == null || this.destination == null) { + throw new MessagingConfigurationException("Either a 'jmsTemplate' or " + + "*both* 'connectionFactory' and 'destination' are required."); + } + this.jmsTemplate = new JmsTemplate(); + this.jmsTemplate.setConnectionFactory(this.connectionFactory); + this.jmsTemplate.setDefaultDestination(this.destination); + } + } + + @Override + protected boolean doSendObject(Object object) throws Exception { + this.jmsTemplate.convertAndSend(object); + return true; + } + +}