zoukankan      html  css  js  c++  java
  • 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端输出:

  • 相关阅读:
    Qt图片显示
    C# 对话框使用整理
    C# AutoResetEvent 使用整理
    C++ 模板
    superset使用
    superset部署
    kafka修改topic副本数
    c# 生成自定义图片
    c# 解决Randoms伪随机重复问题
    c# 图片加水印
  • 原文地址:https://www.cnblogs.com/kavlez/p/4071937.html
Copyright © 2011-2022 走看看