Authored by 胡古飞

skd不依赖core包

... ... @@ -4,14 +4,14 @@ import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.yoho.core.message.YhProducerTemplate;
import com.yoho.error.utils.LocalhostIpFetcher;
import com.yoho.message.sdk.common.event.MessageCenterCommonEvent;
import com.yoho.message.sdk.common.mq.MessageProducerTemplate;
/**
* 消息中心事件处理器,上报到MQ
... ... @@ -22,8 +22,8 @@ import com.yoho.message.sdk.common.event.MessageCenterCommonEvent;
@Component
public class MessageCenterMqEventHandler implements ApplicationListener<MessageCenterCommonEvent> {
@Resource(name = "mc-producerTemplate")
private YhProducerTemplate mcProducerTemplate;
@Resource(name = "messageProducerTemplate")
private MessageProducerTemplate messageProducerTemplate;
private static final String MessageCenterTopic = "message-center-topic";
... ... @@ -49,8 +49,7 @@ public class MessageCenterMqEventHandler implements ApplicationListener<MessageC
event.setParams(params);
}
params.put("ip", localIP);
mcProducerTemplate.send(MessageCenterTopic, event);
messageProducerTemplate.send(MessageCenterTopic, event);
}
}
... ...
package com.yoho.message.sdk.common.mq;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import javax.annotation.PostConstruct;
import org.apache.commons.codec.Charsets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import com.alibaba.fastjson.JSON;
/**
* 发送消息到Message broker中 当前是发送到topic中 Created by chang@yoho.cn on 2015/11/4.
*/
public class MessageProducerTemplate {
private final Logger logger = LoggerFactory.getLogger(MessageProducerTemplate.class);
private final Logger asynclogger = LoggerFactory.getLogger("asyncproducer");
private final int DEFAULT_QUEUE_SIZE = 8000;
private final LinkedBlockingQueue<Entry> asyncSendQueues = new LinkedBlockingQueue<>(DEFAULT_QUEUE_SIZE);
private boolean asyncEnable = false;// 针对单个bean的开关,可以在配置文件里面注入,默认true
private AmqpTemplate amqpTemplate;
private final String TOPIC_EXCHAGE = "amq.topic";
@PostConstruct
void init() {
new Thread("MessageProducerTemplate-thread") {
@Override
public void run() {
try {
Entry e;
while ((e = asyncSendQueues.take()) != null) {
try {
amqpTemplate.send(e.topicExchange, e.topic, e.amqpMsg);
} catch (Exception exception) {
asynclogger.error("mq send exception,{}", exception);
}
}
} catch (Exception e) {
asynclogger.error("YhProducerTemplate-thread error ,{}", e);
}
}
}.start();
}
/**
* 延迟发送消息
*
* @param topic
* 消息的主题
* @param object
* 消息对象,框架会自动转化为JSON
* @param attributes
* 消息属性
* @param delayInMinutes
* 延迟多少分钟,必须和consumers配置的保持一致
*/
public void send(String topic, Object object, Map<String, Object> attributes, int delayInMinutes) {
// yoho_delay.2m.topic
String sent_topic = "yoho_delay." + delayInMinutes + "m." + topic;
this.send(sent_topic, object, attributes);
}
/**
* 发送消息
*
* @param topic
* 消息主题
* @param object
* 消息体,java对象
*/
public void send(String topic, Object object) {
this.send(topic, object, null);
}
/**
* 发送消息
*
* @param topic
* 消息主题
* @param object
* 消息体 会转化为JSON,然后发送
* @param attributes
* 消息属性
*/
public void send(String topic, Object object, Map<String, Object> attributes) {
// 消息的属性
MessageProperties properties = new MessageProperties();
properties.setContentType("text");
if (attributes != null) {
for (Map.Entry<String, Object> entry : attributes.entrySet()) {
properties.setHeader(entry.getKey(), entry.getValue());
}
}
// 消息体
byte[] body = JSON.toJSONString(object).getBytes(Charsets.toCharset("UTF-8"));
Message amqpMsg = new Message(body, properties);
if (asyncEnable) {
asycnSend(TOPIC_EXCHAGE, topic, amqpMsg);
} else {
this.amqpTemplate.send(TOPIC_EXCHAGE, topic, amqpMsg);
}
logger.debug("send mq message success. exchange:{}, topic:{}, message:{}", TOPIC_EXCHAGE, topic, object);
}
private void asycnSend(String topicExchange, String topic, Message amqpMsg) {
if (!asyncSendQueues.offer(new Entry(topicExchange, topic, amqpMsg))) {
try {
asynclogger.error("mq queue is full ,and topicExchange={} ,topic={},amqpMsg={}", topicExchange, topic, new String(amqpMsg.getBody(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
asynclogger.error(e.getMessage());
}
}
}
private class Entry {
private String topicExchange;
private String topic;
private Message amqpMsg;
public Entry(String topicExchange, String topic, Message amqpMsg) {
this.amqpMsg = amqpMsg;
this.topic = topic;
this.topicExchange = topicExchange;
}
}
public boolean isAsyncEnable() {
return asyncEnable;
}
public void setAsyncEnable(boolean asyncEnable) {
this.asyncEnable = asyncEnable;
}
// spring setting, do not call
public void setAmqpTemplate(AmqpTemplate amqpTemplate) {
this.amqpTemplate = amqpTemplate;
}
}
... ...
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mc-simpleMessageConverter" class="org.springframework.amqp.support.converter.SimpleMessageConverter"/>
<rabbit:connection-factory id="mc-rabbitmq-connectionFactory" addresses="${rabbit_message_center:rabbitmq-common-aws.yohoops.org:5672}"
username="${rabbit_message_center_user:yoho}" password="${rabbit_message_center_password:yoho}" virtual-host="${rabbit_message_center_vhost:yoho}" />
<rabbit:template id="mc-amqpTemplate" connection-factory="mc-rabbitmq-connectionFactory" message-converter="mc-simpleMessageConverter"/>
<!--消息中心发送消息的 producerTemplate-->
<bean id="mc-producerTemplate" class="com.yoho.core.message.YhProducerTemplate" primary="false">
<property name="amqpTemplate" ref="mc-amqpTemplate"/>
</bean>
</beans>
... ... @@ -10,7 +10,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.yoho.message.sdk.service.order.ISendOrderMessage;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:META-INF/spring/spring-*.xml" })
@ContextConfiguration(locations = { "classpath*:META-INF/spring/spring*.xml" })
public class TestSendOrderMessage {
@Resource
... ...
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
... ... @@ -16,9 +15,17 @@
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<!-- Spring扫描目录,通过注解的方式注入bean,只扫描本工程的目录 -->
<context:component-scan base-package="com.yoho.*" />
<task:annotation-driven/>
<!-- 打开aop 注解 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- Spring扫描目录,通过注解的方式注入bean,只扫描本工程的目录 -->
<context:component-scan base-package="com.yoho.message.sdk.*" />
<task:annotation-driven />
<!-- 打开aop 注解 -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:message-sdk.properties</value>
</property>
<property name="fileEncoding" value="utf-8" />
</bean>
</beans>
\ No newline at end of file
... ...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xmlns="http://www.springframework.org/schema/beans"
... ... @@ -6,24 +7,16 @@
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mc-simpleMessageConverter" class="org.springframework.amqp.support.converter.SimpleMessageConverter"/>
<bean id="messageSimpleMessageConverter" class="org.springframework.amqp.support.converter.SimpleMessageConverter"/>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:test-message-sdk.properties</value>
</property>
<property name="fileEncoding" value="utf-8" />
</bean>
<rabbit:connection-factory id="mc-rabbitmq-connectionFactory" addresses="${rabbit_message_center:rabbitmq-common-aws.yohoops.org:5672}"
<rabbit:connection-factory id="message-rabbitmq-connectionFactory" addresses="${rabbit_message_center:192.168.102.211:5672}"
username="${rabbit_message_center_user:yoho}" password="${rabbit_message_center_password:yoho}" virtual-host="${rabbit_message_center_vhost:yoho}" />
<rabbit:template id="mc-amqpTemplate" connection-factory="mc-rabbitmq-connectionFactory" message-converter="mc-simpleMessageConverter"/>
<rabbit:template id="messageAmqpTemplate" connection-factory="message-rabbitmq-connectionFactory" message-converter="messageSimpleMessageConverter"/>
<!--消息中心发送消息的 producerTemplate-->
<bean id="mc-producerTemplate" class="com.yoho.core.message.YhProducerTemplate" primary="false">
<property name="amqpTemplate" ref="mc-amqpTemplate"/>
<bean id="messageProducerTemplate" class="com.yoho.message.sdk.common.mq.MessageProducerTemplate" primary="false">
<property name="amqpTemplate" ref="messageAmqpTemplate"/>
</bean>
</beans>
</beans>
\ No newline at end of file
... ...
#rabbit address
rabbit_host=192.168.102.216:5672
rabbit_user=admin1
rabbit_password=123qwe
rabbit_vhost=/yohosearch
rabbit_concurrent=8
#rabbit address
rabbit_message_center=192.168.102.211:5672
rabbit_message_center_user=yoho
rabbit_message_center_password=yoho
rabbit_message_center_vhost=yoho
zkAddress=192.168.102.216:2181
\ No newline at end of file
rabbit_message_center_vhost=yoho
\ No newline at end of file
... ...