activeMQ 使用 用activeMQ做RPC呼叫demo
阿新 • • 發佈:2019-02-06
1:下載activemq,進入斌資料夾執行activemq.bat。在http://127.0.0.1:8161/admin 密碼是admin 賬戶admin 可以看到訊息佇列
2:pom
==
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-core</artifactId> <version>5.7.0</version> <exclusions> <exclusion> <artifactId>spring-context</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>3.0.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jms --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>4.3.2.RELEASE</version> </dependency>
==
3 服務端
==
package com.wang; public interface PersonService { String sayHello(); } package com.wang; public class PersonServiceImpl implements PersonService { public String sayHello() { return "hello word"; } } public class ServerMain { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext( "service.xml"); } }
service.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean class="org.springframework.jms.listener.SimpleMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="myQueue" /> <property name="concurrentConsumers" value="3" /> <property name="messageListener" ref="rpcService" /> <property name="taskExecutor" ref="threadPool" /> </bean> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://127.0.0.1:61616" /> </bean> <bean id="myQueue" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg value="rpcQueue" /> </bean> <bean id="rpcService" class="org.springframework.jms.remoting.JmsInvokerServiceExporter"> <property name="serviceInterface" value="com.wang.PersonService" /> <property name="service"> <bean class="com.wang.PersonServiceImpl" /> </property> </bean> <bean id="threadPool" class="org.springframework.core.task.SimpleAsyncTaskExecutor"> <property name="daemon" value="true" /> <property name="concurrencyLimit" value="300" /> <property name="threadNamePrefix" value="SERVICE" /> </bean> </beans>
==
4 客戶端
==
package com.wang;
public interface PersonService {
String sayHello();
}
public class Client {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("client.xml");
PersonService service = (PersonService) ctx.getBean("rpcService");
String result = service.sayHello();
System.out.println(result);
}
}
client.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://127.0.0.1:61616" />
</bean>
<bean id="myQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="rpcQueue" />
</bean>
<bean id="rpcService" class="org.springframework.jms.remoting.JmsInvokerProxyFactoryBean">
<property name="serviceInterface" value="com.wang.PersonService" />
<property name="connectionFactory" ref="connectionFactory" />
<property name="queue" ref="myQueue" />
</bean>
</beans>
==
6:說明,在程式碼中可以清楚看到客戶端 服務段。通過執行程式碼,觀察http://127.0.0.1:8161/admin可以看到,在mq中,執行客戶端,相當於mq的一個生產者,允許程式碼的服務端,相當於消費者。以此實現用“推”的模式,實現看起來是“拉”的效果