Skip to content

Instantly share code, notes, and snippets.

@garyrussell
Last active August 29, 2015 14:13
Show Gist options
  • Save garyrussell/c969c00af9c9ce1a4d00 to your computer and use it in GitHub Desktop.
Save garyrussell/c969c00af9c9ce1a4d00 to your computer and use it in GitHub Desktop.
DSL Gateway Per Service
/*
* Copyright 2015 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 foo;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.core.MessagingTemplate;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.messaging.MessageChannel;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import foo.InlineGateways.FooConfig;
/**
* @author Gary Russell
* @since 4.2
*
*/
@ContextConfiguration(classes=FooConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class InlineGateways {
@Autowired
@Qualifier("gatewayFlow.input")
private MessageChannel start;
@Autowired
private ApplicationContext ctx;
@Test
public void test() {
MessagingTemplate template = new MessagingTemplate(start);
assertEquals("foofoofoofoofoofoofoofoo", template.convertSendAndReceive("foo", String.class));
}
@Configuration
@EnableIntegration
@ComponentScan
public static class FooConfig {
@Bean
public IntegrationFlow gatewayFlow() {
return f -> f
.gateway("s1", e -> e.errorChannel("e1"))
.gateway("s2", e -> e.errorChannel("e2"))
.gateway("s3", e -> e.errorChannel("e3"));
}
@Bean
public MessageChannel e1() {
return new DirectChannel();
}
@Bean
public MessageChannel e2() {
return new DirectChannel();
}
@Bean
public MessageChannel e3() {
return new DirectChannel();
}
}
@MessageEndpoint
public static class Service1 {
@ServiceActivator(inputChannel="s1")
public String handle(String in) {
return in + in;
}
}
@MessageEndpoint
public static class Service2 {
@ServiceActivator(inputChannel="s2")
public String handle(String in) {
return in + in;
}
}
@MessageEndpoint
public static class Service3 {
@ServiceActivator(inputChannel="s3")
public String handle(String in) {
return in + in;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment