mq改成真实业务逻辑

This commit is contained in:
shuzheng 2016-11-26 21:53:45 +08:00
parent e608bc9fae
commit 39565f716f
3 changed files with 59 additions and 20 deletions

View File

@ -1,5 +1,8 @@
package com.zheng.cms.jms;
import com.zheng.cms.dao.model.User;
import com.zheng.cms.service.UserService;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -15,23 +18,30 @@ import javax.jms.TextMessage;
*/
public class defaultQueueMessageListener implements MessageListener {
private static Logger _log = LoggerFactory.getLogger(defaultQueueMessageListener.class);
private static Logger _log = LoggerFactory.getLogger(defaultQueueMessageListener.class);
@Autowired
ThreadPoolTaskExecutor threadPoolTaskExecutor;
@Autowired
ThreadPoolTaskExecutor threadPoolTaskExecutor;
public void onMessage(final Message message) {
// 使用线程池多线程处理
threadPoolTaskExecutor.execute(new Runnable() {
public void run() {
TextMessage textMessage = (TextMessage) message;
try {
_log.info("cms-mq接收到{}", textMessage.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Autowired
UserService userService;
public void onMessage(final Message message) {
// 使用线程池多线程处理
threadPoolTaskExecutor.execute(new Runnable() {
public void run() {
TextMessage textMessage = (TextMessage) message;
try {
String text = textMessage.getText();
JSONObject json = JSONObject.fromObject(text);
User user = (User) JSONObject.toBean(json, User.class);
userService.getMapper().insertSelective(user);
_log.info("cms-mq接收到{}", text);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}

View File

@ -1,6 +1,8 @@
package com.zheng.cms.controller;
import com.zheng.cms.dao.model.User;
import com.zheng.common.util.JmsUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -32,12 +34,29 @@ public class ActiveMQController extends BaseController {
@ResponseBody
public Object send() {
long start = System.currentTimeMillis();
for (int i = 0; i < 100; i ++) {
_log.info("发送消息" + (i + 1));
JmsUtil.sendMessage(jmsQueueTemplate, defaultQueueDestination, "消息" + (i + 1));
User user = null;
for (int i = 1; i <= 10000; i ++) {
user = new User();
user.setUsername("用户" + i);
user.setPassword("123456");
user.setNickname("昵称");
user.setSex(1);
user.setCtime(System.currentTimeMillis());
user.setContent("用户描述");
JmsUtil.sendMessage(jmsQueueTemplate, defaultQueueDestination, JSONObject.fromObject(user).toString());
}
_log.info("发送消息消耗时间" + (System.currentTimeMillis() - start));
return "success";
}
public static void main(String[] args) {
User user = new User();
user.setUsername("用户");
user.setPassword("123456");
user.setNickname("昵称");
user.setSex(1);
user.setCtime(System.currentTimeMillis());
user.setContent("用户描述");
System.out.println(JSONObject.fromObject(user).toString());
}
}

View File

@ -1,5 +1,8 @@
package com.zheng.cms.jms;
import com.zheng.cms.dao.model.User;
import com.zheng.cms.service.UserService;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -20,13 +23,20 @@ public class defaultQueueMessageListener implements MessageListener {
@Autowired
ThreadPoolTaskExecutor threadPoolTaskExecutor;
@Autowired
UserService userService;
public void onMessage(final Message message) {
// 使用线程池多线程处理
threadPoolTaskExecutor.execute(new Runnable() {
public void run() {
TextMessage textMessage = (TextMessage) message;
try {
_log.info("cms-web接收到{}", textMessage.getText());
String text = textMessage.getText();
JSONObject json = JSONObject.fromObject(text);
User user = (User) JSONObject.toBean(json, User.class);
userService.getMapper().insertSelective(user);
_log.info("cms-web接收到{}", text);
} catch (Exception e) {
e.printStackTrace();
}