1、封装MessageListener
public abstract class AbstractDdshowMessageListener implements MessageListener {
protected Logger logger = LoggerFactory.getLogger(getClass());
@Override
public void onMessage(Message message){
logger.debug("Received a message: {}", message.toString());
StopWatch stopWatch = new Slf4JStopWatch(this.getClass().getName(),
LoggerFactory.getLogger(LoggingStopWatch.DEFAULT_LOGGER_NAME));
try {
doOnMessage(message);
}finally {
stopWatch.stop();
}
}
protected abstract void doOnMessage(Message message);
}
2、SendGiftMessageListener 生产者
AbstractDdshowMessageListener 的实现
public class SendGiftMessageListener extends AbstractDdshowMessageListener implements
InitializingBean {
@Autowired
private JmsTemplate jmsTemplate;
@Autowired
@Qualifier("imQueue")
private Queue imQueue;
@Autowired
@Qualifier("screenStatQueue")
private Queue screenStatQueue;
// 本场排行榜统计
jmsTemplate.send(screenStatQueue, new ScreenStatMessage(record.getOriginUserId(),
record.getTargetUserId(), record.getScreenId(), record.getGiftCoins()));
}
3、消息体ScreenStatMessage
public class ScreenStatMessage implements MessageCreator {
public ScreenStatMessage(int originUserId, int targetUserId, int screenId, int coins) {
this.originUserId = originUserId;
this.targetUserId = targetUserId;
this.screenId = screenId;
this.coins = coins;
}
public ScreenStatMessage(MapMessage message) throws IllegalArgumentException {
try {
this.setOriginUserId(message.getInt("originUserId"));
this.setTargetUserId(message.getInt("targetUserId"));
this.setScreenId(message.getInt("screenId"));
this.setCoins(message.getInt("coins"));
} catch (JMSException ex) {
throw new IllegalArgumentException(ex);
}
}
public ScreenStatMessage() {
}
@Override
public String toString() {
return "ScreenStatMessage{" + "originUserId=" + originUserId + ", targetUserId="
+ targetUserId + ", screenId=" + screenId + ", coins=" + coins + '}';
}
@Override
public Message createMessage(Session session) throws JMSException {
MapMessage message = session.createMapMessage();
message.setInt("originUserId", this.getOriginUserId());
message.setInt("targetUserId", this.getTargetUserId());
message.setInt("screenId", this.getScreenId());
message.setInt("coins", this.getCoins());
return message;
}
}
4、消息配置
applicationContext-mq.xml
<amq:queue id="screenStatQueue" physicalName="${jms.destination.queue.screenstat}" />
5、监听器
applicationContext-listener.xml 在consumer中监听消息,然后发新消息
<context:annotation-config />
<context:component-scan base-package="com.youku.ddshow" />
<jms:listener-container concurrency="${jms.listener-container.concurrency}" destination-type="queue">
<!-- 礼物赠送记录消息监听者 -->
<jms:listener id="giftSendMessageContainer" destination="${jms.destination.queue.sendgift}"
ref="giftSendMessageListener" />
</jms:listener-container>
<bean id="giftSendMessageListener" class="com.youku.ddshow.consumer.SendGiftMessageListener">
<property name="luckyGiftConfigurations" ref="luckyGiftConfiguration" />
</bean>
6、消费者监听
applicationContext-listener.xml
通过 ${jms.destination.queue.screenstat}
<!-- 场次统计 -->
<jms:listener destination="${jms.destination.queue.screenstat}" ref="screenStatMessageListener"/>
7、消息名称定义
system-online.properties
jms.destination.queue.screenstat=Q.DDSHOW.SCREENSTAT
8、消费者
applicationContext-listener.xml
<!-- 场次统计 -->
<bean id="screenStatMessageListener" class="com.youku.ddshow.consumer.ScreenStatMessageListener" />
ScreenStatMessageListener
public class ScreenStatMessageListener extends AbstractDdshowMessageListener {
@Override
protected void doOnMessage(Message message) {
if (message instanceof MapMessage) {
MapMessage mapMessage = (MapMessage) message;
ScreenStatMessage statMessage = new ScreenStatMessage(mapMessage);
int originUserId = statMessage.getOriginUserId();
int targetUserId = statMessage.getTargetUserId();
int screenId = statMessage.getScreenId();
int coins = statMessage.getCoins();
// 统计
stat(originUserId, targetUserId, screenId, coins);
}
}
}