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