activeMQ 使用 用activeMQ做RPC调用demo


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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值