Skip to content

Instantly share code, notes, and snippets.

@garyrussell
Created May 7, 2014 14:58
Show Gist options
  • Save garyrussell/b49d2681284b89ddd907 to your computer and use it in GitHub Desktop.
Save garyrussell/b49d2681284b89ddd907 to your computer and use it in GitHub Desktop.
JMS Outbound Gateway Spring Boot App
/*
* Copyright 2014 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 bar;
import javax.jms.ConnectionFactory;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.IntegrationComponentScan;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.jms.JmsOutboundGateway;
import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.springframework.jms.listener.adapter.MessageListenerAdapter;
import org.springframework.messaging.MessageChannel;
/**
* @author Gary Russell
* @since 4.0
*
*/
@EnableAutoConfiguration
@EnableIntegration
@IntegrationComponentScan
@Configuration
public class JmsRequestReply {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = new SpringApplication(JmsRequestReply.class).run();
ClientGateway gateway = ctx.getBean(ClientGateway.class);
System.out.println(gateway.sendAndReceive("foo"));
ctx.close();
}
@MessagingGateway(defaultRequestChannel="requests")
public interface ClientGateway {
String sendAndReceive(String toSend);
}
@Bean
public MessageChannel requests() {
return new DirectChannel();
}
@Bean
@ServiceActivator(inputChannel="requests")
public JmsOutboundGateway jmsGateway() {
JmsOutboundGateway gw = new JmsOutboundGateway();
gw.setConnectionFactory(connectionFactory());
gw.setRequestDestinationName("test.out");
gw.setReplyDestinationName("test.in");
gw.setCorrelationKey("JMSCorrelationID");
return gw;
}
@Bean
public ConnectionFactory connectionFactory() {
return new CachingConnectionFactory(new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"));
}
@Bean
public DefaultMessageListenerContainer responder() {
DefaultMessageListenerContainer container = new DefaultMessageListenerContainer();
container.setConnectionFactory(connectionFactory());
container.setDestinationName("test.out");
MessageListenerAdapter adapter = new MessageListenerAdapter(new Object() {
@SuppressWarnings("unused")
public String handleMessage(String in) {
return in.toUpperCase();
}
});
container.setMessageListener(adapter);
return container;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment