消费mq配置 spring-rabbitmq-message.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:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
<!-- 获取配置资源 -->
<!--<context:property-placeholder location="classpath:conf/rabbitmq.properties" />-->
<!-- RabbitMQ start -->
<!-- 连接配置 -->
<rabbit:connection-factory id="connectionFactory" host="${mq.host}" username="${mq.username}"
password="${mq.password}" port="${mq.port}" />
<rabbit:admin connection-factory="connectionFactory"/>
<!-- 消息队列客户端 -->
<!--<rabbit:template id="messageCommonTemplate" exchange="messageCommonExchange" routing-key="messageCommonRoutinekey" connection-factory="connectionFactory" />-->
<!-- queue 队列声明 -->
<!--
durable 是否持久化
exclusive 仅创建者可以使用的私有队列,断开后自动删除
auto-delete 当所有消费端连接断开后,是否自动删除队列 -->
<rabbit:queue name="messageCommonQueuespring" durable="true" auto-delete="false" exclusive="false" />
<!-- 交换机定义 -->
<!--
交换机:一个交换机可以绑定多个队列,一个队列也可以绑定到多个交换机上。
如果没有队列绑定到交换机上,则发送到该交换机上的信息则会丢失。
direct模式:消息与一个特定的路由器完全匹配,才会转发
topic模式:按模式匹配
-->
<rabbit:direct-exchange name="cloud" durable="true" auto-delete="false" >
<rabbit:bindings>
<rabbit:binding queue="messageCommonQueuespring" key="message_1"></rabbit:binding>
<rabbit:binding queue="messageCommonQueuespring" key="message_2"></rabbit:binding>
<rabbit:binding queue="messageCommonQueuespring" key="message_3"></rabbit:binding>
</rabbit:bindings>
</rabbit:direct-exchange>
<bean id="messageService" class="com.rabbitmq.consumer.MessageConsumer"/>
<!-- 配置监听 消费者 -->
<rabbit:listener-container connection-factory="connectionFactory" acknowledge="auto">
<!--
queues 监听队列,多个用逗号分隔
ref 监听器 -->
<rabbit:listener queues="messageCommonQueuespring" ref="messageService"/>
</rabbit:listener-container>
</beans>
启动main方法
@Service
public class RunMain {
public RunMain() throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
try {
Class clazz = Class.forName("com.grpc.service.ConsumerService");
Object obj = clazz.newInstance();
Method mainMethod = clazz.getMethod("main", String[].class);
mainMethod.invoke(obj, (Object)new String[]{});
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
注入类方法
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBeanById(String id){
return applicationContext.getBean(id);
}
public static Object getBeanByClass(Class c){
return applicationContext.getBean(c);
}
public static Map getBeansByClass(Class c){
return applicationContext.getBeansOfType(c);
}
}