?
本文檔使用 PHP中文網(wǎng)手冊 發(fā)布
使用JMS來作為底層的通信協(xié)議透明暴露服務(wù)也是可能的。Spring框架中對JMS的遠(yuǎn)程支持也很基礎(chǔ) - 它在同一線程和同一個非事務(wù) Session
上發(fā)送和接收,這些吞吐量將非常依賴于實現(xiàn)。
The following interface is used on both the server and the client side.
下面的接口可同時用在服務(wù)端和客戶端。
package com.foo; public interface CheckingAccountService { public void cancelAccount(Long accountId); }
對于上面接口的使用在服務(wù)的端簡單實現(xiàn)如下。
package com.foo; public class SimpleCheckingAccountService implements CheckingAccountService { public void cancelAccount(Long accountId) { System.out.println("Cancelling account [" + accountId + "]"); } }
這個包含JMS設(shè)施的bean的配置文件可同時用在客戶端和服務(wù)端。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://ep-t43:61616"/> </bean> <bean id="queue" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="mmm"/> </bean> </beans>
在服務(wù)端你只需要使用JmsInvokerServiceExporter
來暴露服務(wù)對象。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="checkingAccountService" class="org.springframework.jms.remoting.JmsInvokerServiceExporter"> <property name="serviceInterface" value="com.foo.CheckingAccountService"/> <property name="service"> <bean class="com.foo.SimpleCheckingAccountService"/> </property> </bean> <bean class="org.springframework.jms.listener.SimpleMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory"/> <property name="destination" ref="queue"/> <property name="concurrentConsumers" value="3"/> <property name="messageListener" ref="checkingAccountService"/> </bean> </beans>
package com.foo; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Server { public static void main(String[] args) throws Exception { new ClassPathXmlApplicationContext(new String[]{"com/foo/server.xml", "com/foo/jms.xml"}); } }
客戶端僅僅需要創(chuàng)建一個客戶端代理來實現(xiàn)上面的接口(CheckingAccountService
)。根據(jù)后面的bean定義創(chuàng)建的結(jié)果對象可以被注入到其它客戶端對象中,而這個代理會負(fù)責(zé)通過JMS將調(diào)用轉(zhuǎn)發(fā)到服務(wù)端。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="checkingAccountService" class="org.springframework.jms.remoting.JmsInvokerProxyFactoryBean"> <property name="serviceInterface" value="com.foo.CheckingAccountService"/> <property name="connectionFactory" ref="connectionFactory"/> <property name="queue" ref="queue"/> </bean> </beans>
package com.foo; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client { public static void main(String[] args) throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext( new String[] {"com/foo/client.xml", "com/foo/jms.xml"}); CheckingAccountService service = (CheckingAccountService) ctx.getBean("checkingAccountService"); service.cancelAccount(new Long(10)); } }
你可能也希望研究Lingo 項目提供的支持,它(引用到主頁) “... 是一個基于輕量級POJO的遠(yuǎn)程核消息代碼庫,它使用并擴展了Spring框架的遠(yuǎn)程代碼庫以支持JMS。”