• JMS


    现在试试通过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;
    @Service
    public 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端输出:

  • 相关阅读:
    Linux上查找
    Linux进程
    Linux重定向
    Linux上常用的基本命令
    LInux上返回到切换目录前的目录
    【网络知识之一】4/7层网络模型
    【操作系统之十五】iptables黑白名单、自定义链、网络防火墙、常用动作
    【操作系统之十四】iptables扩展模块
    【操作系统之十三】Netfilter与iptables
    【操作系统之十二】分支预测、CPU亲和性(affinity)
  • 原文地址:https://www.cnblogs.com/kavlez/p/4071937.html
Copyright © 2020-2023  润新知