Spring之JMS

来源:互联网 发布:淘宝宝贝排名下降了 编辑:程序博客网 时间:2024/04/27 23:16


使用JMS作为底层通信协议也能暴露service。Spring框架的JMS远程支持是非常简单的。在同一线程和同一非事物会话上发送和接受,并且吞吐量也将是非常依赖于实现的。注意这些单线程和非事物约束仅应用于Spring的jms远程支持。


下面的接口用于服务器端和客户端。


package com.foo;public interface CheckingAccountService {    public void cancelAccount(Long accountId);}


下面是上述接口的简单实现用于服务器端:

package com.foo;public class SimpleCheckingAccountService implements CheckingAccountService {    public void cancelAccount(Long accountId) {        System.out.println("Cancelling account [" + accountId + "]");    }}

这个配置文件包含了JMS使用的beans,用于客户端和服务器端。


<?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.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>


21.6.1 服务器端配置


在服务器端,你仅需要使用JmsInvokerServiceExporter暴露你的业务对象。


<?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.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"});    }}



21.6.2 客户端配置


客户端仅需要创建一个客户端的代理,将实现通过协议的上述接口(CheckingAccountService)。由下面bean定义创建的结果对象可以注入到另一个客户端对象,并且代理将通过JMS保持对服务器端对象的调用。


<?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.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));    }}





0 0
原创粉丝点击