现在试试通过JMS,在应用程序之间发送消息。先看看sPRing提供的RPC方案(其实还有其他方案,只是没见过谁用)。需要使用到这两个类:·org.springframework.jms.remoting.JmsInvokerServiceExporter将bean导出为基于消息的服务·org.springframework.jms.remoting.JmsInvokerProxyFactoryBean让客户端调用服务
比较一下JmsInvokerServiceExporter和RmiServiceExporter:

package pac.testcase.jms;public interface JmsRmiService {    String doServe(String requestedNum);}package pac.testcase.jms;import org.springframework.stereotype.Service;@Servicepublic class JmsRmiServiceImpl implements JmsRmiService {                                                                                                                                                         public String doServe(String content) {        System.out.println(content.concat(" has been requested!!"));        return "your message::".concat(content).concat(":::length:")+content.length();    }}将这个pojo声明为服务,在spring配置文件中配置:
<bean id="serverService" class="org.springframework.jms.remoting.JmsInvokerServiceExporter" p:serviceInterface="pac.testcase.jms.JmsRmiService" p:service-ref="JmsRmiServiceImpl"></bean>
需将他设置为jms监听器,配置方法和一般的jmsMessageListener的配置相同:
<amq:connectionFactory id="jmsFactory" /><jms:listener-container destination-type="queue" connection-factory="jmsFactory" concurrency="3" container-type="simple"> <jms:listener destination="sparta" ref="serverService" /></jms:listener-container>
container-type有simple和default,根据不同的type也可以使用task-Executor,这里先简单记录一下。
先启动jms broker再启动:
new ClassPathxmlapplicationContext("classpath:applicationContext-*.xml").getBean(JmsRmiService.class);
client这边我需要一个调用代理帮我去调用接口,也就是JmsInvokerProxyFactoryBean;配置如下:
<amq:connectionFactory id="connectionFactory" /><bean id="clientService" class="org.springframework.jms.remoting.JmsInvokerProxyFactoryBean" p:serviceInterface="pac.test.jms.SenderRmiService" p:connectionFactory-ref="connectionFactory" p:queueName="sparta"/>
配置中的serviceInterface是client端中根据要调用的方法创建的一个接口。
main方法试着调用看看:
public static void main(String[] args) {    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");    SenderRmiService service = (SenderRmiService)context.getBean("clientService");    System.out.println(service.doServe("这才是斯巴达!!"));}server端输出: client端输出:
client端输出:
新闻热点
疑难解答