JMS-based RPC

Source: Internet
Author: User
Tags amq

Now try sending messages between applications through JMS.
Let's take a look at the RPC solution provided by spring (in fact there are other solutions, but we have never seen anyone using it ).
You need to use these two classes:
· Org. springframework. JMS. remoting. jmsinvokerserviceexporter exports beans as message-based services.
· Org. springframework. JMS. remoting. jmsinvokerproxyfactorybean allows the client to call the service

 

Compare jmsinvokerserviceexporter and 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();    }}

 

Declare this pojo as a service and configure it in the spring configuration file:

<bean id="serverService" class="org.springframework.jms.remoting.JmsInvokerServiceExporter"    p:serviceInterface="pac.testcase.jms.JmsRmiService"    p:service-ref="JmsRmiServiceImpl"></bean>

 

Set it as a JMS listener. The configuration method is the same as that of 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 has simple and default values. You can also use task-executor Based on Different types. Here we will record it briefly.

Start the JMS broker and then start it:

new ClassPathXmlApplicationContext("classpath:applicationContext-*.xml").getBean(JmsRmiService.class);

 

On the client side, I need a call proxy to help me call the interface, that is, jmsinvokerproxyfactorybean;
The configuration is as follows:

<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"/>

The serviceinterface in the configuration is an interface created in the client based on the method to be called.

 

Try to call the main method:

Public static void main (string [] ARGs) {applicationcontext context = new classpathxmlapplicationcontext ("classpath: applicationcontext. XML "); senderrmiservice service = (senderrmiservice) context. getbean ("clientservice"); system. out. println (service. doserve ("this is Sparta !! "));}

 

Server output:

Client output:

JMS-based RPC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.