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的一个生产者,允许代码的服务端,相当于消费者。以此实现用“推”的模式,实现看起来是“拉”的效果