領先一步
VMware 提供培訓和認證,助您加速進步。
瞭解更多在我最近的帖子中,我曾提到Spring Integration的Subversion倉庫將很快公開訪問,很高興現在提供該連結。您可以使用以下命令檢出專案
svn co https://anonsvn.springframework.org/svn/spring-integration/base/trunk spring-integration
如果檢出成功,您應該會看到以下目錄結構
spring-integration/
+--build-spring-integration/
+--spring-build/
+--spring-integration-core/
+--spring-integration-samples/
我想借此機會介紹一下'spring-integration-samples'中的一些示例。請注意,這個專案無疑仍在開發中(目前是0.5 SNAPSHOT版本),但這些示例應該能讓您瞭解程式設計模型的成型,我非常期待收到一些反饋。
public class HelloService {
public String sayHello(String name) {
return "Hello " + name;
}
}
此示例使用基於XML的配置來配置訊息端點(我們將在下一個示例中看到註解方法)
<endpoint input-channel="inputChannel"
default-output-channel="outputChannel"
handler-ref="helloService"
handler-method="sayHello"/>
您可以看到'handler-ref'只是指向一個Spring管理的bean。如果您使用Spring的MessageListenerAdapter來非同步接收JMS訊息,那麼這應該看起來很熟悉——特別是如果您正在使用Spring 2.5的新jms名稱空間和“jms:listener”元素。最後,HelloWorldDemo啟動應用程式上下文,然後與通道進行互動
ChannelRegistry channelRegistry = (ChannelRegistry) context.getBean(MessageBusParser.MESSAGE_BUS_BEAN_NAME);
MessageChannel inputChannel = channelRegistry.lookupChannel("inputChannel");
MessageChannel outputChannel = channelRegistry.lookupChannel("outputChannel");
inputChannel.send(new StringMessage(1, "World"));
System.out.println(outputChannel.receive().getPayload());
該示例涉及查詢MessageBus bean——它實現了ChannelRegistry介面。然而,在非演示的“真實世界”場景中,任何訪問通道的元件都可以透過依賴注入來提供登錄檔。它只需要實現ChannelRegistryAware(或使用@Autowired)。這與Spring中其他地方使用的相同方法類似——例如ApplicationEventPublisherAware。
@MessageEndpoint(defaultOutput="quotes")
public class QuotePublisher {
@Polled(period=300)
public Quote getQuote() {
BigDecimal price = new BigDecimal(new Random().nextDouble() * 100);
return new Quote(generateTicker(), price.setScale(2, RoundingMode.HALF_EVEN));
}
private String generateTicker() {
// randomly generates 3-letter tickers
}
}
在接收端,有一個@Subscriber註解
public class QuoteSubscriber {
@Subscriber(channel="quotes")
public void log(Object o) {
System.out.println(o);
}
}
這裡是註冊註解後處理器和2個Spring管理的bean的XML(請注意,此示例使用'spring-integration'模式作為主要名稱空間。
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<message-bus/>
<annotation-driven/>
<channel id="quotes"/>
<beans:bean id="publisher" class="org.springframework.integration.samples.quote.QuotePublisher"/>
<beans:bean id="subscriber" class="org.springframework.integration.samples.quote.QuoteSubscriber"/>
</beans:beans>
順便說一下,'annotation-driven'元素還啟用了@Publisher註解,該註解會觸發為任何被註解方法的返回值的非同步傳送到通道的AOP建議的建立。
@MessageEndpoint
public class Counter {
private AtomicInteger count = new AtomicInteger();
@Polled(period=3000)
public int getNumber() {
return count.incrementAndGet();
}
@Router
public String resolveChannel(int i) {
if (i % 2 == 0) {
return "even";
}
return "odd";
}
}
在這些通道的接收端,我們有兩個不同的方法,它們只是記錄訊息的有效負載
@Component
public class NumberLogger {
@Subscriber(channel="even")
public void even(int i) {
System.out.println("even: " + i);
}
@Subscriber(channel="odd")
public void odd(int i) {
System.out.println("odd: " + i);
}
}
順便說一下,請注意NumberLogger被註解為Spring的@Component。@MessageEndpoint註解也包含@Component作為元註解。因此,它們都是“原型”,並且可以透過Spring 2.5的類路徑掃描進行自動檢測。此示例的XML極其簡單
<context:component-scan base-package="org.springframework.integration.samples.oddeven"/>
<message-bus auto-create-channels="true"/>
<annotation-driven/>