Más contenido relacionado
La actualidad más candente (20)
Similar a Spring integration概要 (20)
Spring integration概要
- 10. Message Channel
•
MessageChannel
public interface MessageChannel {
boolean send(Message? message);
! boolean send(Message? message, long timeout);
}
•
送信のみ定義。
PollableChannel
public interface PollableChannel extends MessageChannel {
Message? receive();
! Message? receive(long timeout);
}
•
能動的に受信する。
SubscribableChannel
public interface SubscribableChannel extends MessageChannel {
boolean subscribe(MessageHandler handler);
boolean unsubscribe(MessageHandler handler);
}
10
登録したHandler
に配信される。
- 15. 下準備
pom.xml
!-- Spring Integration --
dependency
groupIdorg.springframework.integration/groupId
artifactIdspring-integration-core/artifactId
version2.2.6.RELEASE/version
/dependency
jarファイル
15
- 16. Hello World:実装
bean定義ファイル
int:channel id=inputChannel/
int:channel id=outputChannel
int:queue capacity=10/
/int:channel
!
!
1. 入出力Channelを定義。
2. Service Activatorを定義。
!-- inputChannel = HelloService#sayHello = outputChannel --
int:service-activator input-channel=inputChannel
output-channel=outputChannel
ref=helloService
3. Serviceはbeanとして定義。
method=sayHello/
beans:bean id=helloService class=“net.spring.HelloService/
HelloService.java
public class HelloService {
!
}
public String sayHello(String name) {
return “Hello, + name;
}
16
- 17. Hello World:テスト
@Test
public void helloWorldTest() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(/METAINF/spring/integration/helloWorld.xml);
!
// 送信メッセージ
MessageString message = MessageBuilder.String withPayload(Kuro).build();
!
// 送信
MessageChannel inputChannel = applicationContext.getBean(“inputChannel,
MessageChannel.class);
inputChannel.send(message);
!
// 受信
PollableChannel outputChannel = applicationContext.getBean(outputChannel,
PollableChannel.class);
MessageString output = (MessageString) outputChannel.receive();
!
// 確認
logger.debug(output: + output.getPayload());
assertThat(output.getPayload(), is(Hello, Kuro));
}
17
- 20. Hello World:同期Gateway
HelloWorldServiceGateway.java
1. interfaseを定義。
public interface HelloWorldServiceGateway {
public String sayHello(String name);
}
2. request-channelとreply-channelに
入力と出力Channelを設定。
bean定義ファイル
!-- HelloWorldService 同期Gateway --
int:gateway id=HelloWorldServiceGateway
default-request-channel=inputChannel default-reply-channel=outputChannel
service-interface=“net.spring.gateway.HelloWorldServiceGateway /
20
- 21. Hello World:同期Gateway
HelloWorldTest.java
@Test
public void helloWorldServiceGatewayTest() throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
/META-INF/spring/integration/helloWorld.xml);
!
HelloWorldServiceGateway gateway = applicationContext
.getBean(HelloWorldServiceGateway, HelloWorldServiceGateway.class);
String message = gateway.sayHello(Kuro);
呼出がシンプルになる。
!
assertThat(message, is(Hello, Kuro));
}
21
- 24. Hello World:非同期Gateway
HelloWorldTest.java
@Test
public void helloWorldServiceGatewayAsyncTest() throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(/
META-INF/spring/integration/helloWorld.xml);
!
HelloWorldServiceGatewayAsync gateway = applicationContext
.getBean(HelloWorldServiceGatewayAsync,
HelloWorldServiceGatewayAsync.class);
FutureString future = gateway.sayHello(Kuro);
!
String message = null;
while (true) {
非同期で結果を取得。
if (future.isDone()) {
message = future.get();
break;
}
}
assertThat(message, is(Hello, Kuro));
}
24
- 26. Hello World:File Adapter
bean定義ファイル
!-- HelloWorldScervice FileAdapter --
int-file:inbound-channel-adapter id=fileAdapter
directory=file:${java.io.tmpdir}
channel=filesInChannel filename-pattern=hello-world-*.txt
int:poller fixed-rate=1000 /
/int-file:inbound-channel-adapter
!
int-file:file-to-string-transformer delete-files=true charset=UTF-8
input-channel=filesInChannel output-channel=inputChannel /
26
- 27. Hello World:File Adapter
HelloWorldTest.java
@Test
public void helloWorldServiceFileAdapterTest() throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(/METAINF/spring/integration/helloWorld.xml);
!
// ポーリングされるディレクトリにファイルを出力する。
String tmpdirPath = System.getProperty(java.io.tmpdir);
String inputFilePath = tmpdirPath + hello-world-kuro.txt;
File nameFile = new File(inputFilePath);
OutputStream outputStream = new FileOutputStream(nameFile);
outputStream.write(Kuro.getBytes());
outputStream.close();
// 受信
PollableChannel outputChannel = applicationContext
.getBean(outputChannel, PollableChannel.class);
MessageString output = (MessageString) outputChannel.receive();
assertThat(output.getPayload(), is(Hello, Kuro));
}
27
- 38. まとめ
Spring Integration
• EIPの参照実装
• Spring Integrationの基本
• Message
• MessageChannel
• Endpoint
• Message
• Header
• Payload
MessageChannel
• P2P
• Pub/Sub
• Endpoint
• ServiceActivator
• 呼出
• Gateway
• Channel Adapter
• 他製品
• Mule ESB
• Apache Camel
•
•
38
- 39. 参考リンク集
•
Spring Integration
http://projects.spring.io/spring-integration/
•
Spring Integration Samples
https://github.com/spring-projects/spring-integration-samples
•
Spring Integration Extensions
https://github.com/spring-projects/spring-integration-extensions
•
Spring Integration and EIP Introduction
http://www.slideshare.net/iweinfuld/spring-integration-and-eip-introduction
•
Light-weight, Open-source Integration: Apache Camel vs. Spring Integration
http://java.dzone.com/articles/light-weight-open-source
•
Which Integration Framework to use ‒ Spring Integration, Mule ESB or Apache Camel?
http://www.kai-waehner.de/blog/2012/01/10/spoilt-for-choice-which-integration-framework-to-use-spring-integration-muleesb-or-apache-camel/
•
Apache Camel
http://camel.apache.org/
•
日本Apache Camelユーザー会
http://sourceforge.jp/projects/cameluserjp/wiki/FrontPage
•
Mule ESB
http://www.mulesoft.org/
39