Commit a0c7baf6 authored by Huang's avatar Huang
Browse files

no commit message

parent 05beecd0
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.jeespring</groupId>
<artifactId>jeespring-mq</artifactId>
<name>jeespring-mq</name>
<description>jeespring-mq</description>
<version>1.0.0</version>
<packaging>jar</packaging>
<parent>
<groupId>com.jeespring</groupId>
<artifactId>jeespring</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.jeespring</groupId>
<artifactId>jeespring-framework</artifactId>
<version>1.0.0</version>
</dependency>
<!-- activemq support -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
</project>
\ No newline at end of file
package com.company.project.modules.activeMQ;
import com.jeespring.common.utils.SendMailUtil;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 消息消费者.
* @author 黄炳桂 516821420@qq.com
* @version v.0.1
* @date 2016年8月23日
*/
@Component
public class CompanyConsumer {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = CompanyProducer.ActiveMQQueueKey)
public void receiveQueue(String text) {
System.out.println(dateFormat.format(new Date()) + " | " +"ActiveMQ Consumer :"+ CompanyProducer.ActiveMQQueueKey+":收到的报文为:"+text);
}
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = CompanyProducer.ActiveMQQueueKeyA)
public void receiveQueueA(String text) {
System.out.println(dateFormat.format(new Date()) + " | " +"ActiveMQ Consumer :"+ CompanyProducer.ActiveMQQueueKeyA+":收到的报文为:"+text);
}
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = CompanyProducer.ActiveMQQueueKeyB)
@SendTo("company.out.queue")
public String receiveQueueB(String text) {
System.out.println(dateFormat.format(new Date()) + " | " +"ActiveMQ Consumer :"+ CompanyProducer.ActiveMQQueueKeyA+"收到的报文为:"+text);
return "return message:"+text;
}
}
\ No newline at end of file
package com.company.project.modules.activeMQ;
import com.jeespring.common.redis.RedisUtils;
import org.apache.activemq.command.ActiveMQQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.jms.Destination;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 消息生产者.
* @author 黄炳桂 516821420@qq.com
* @version v.0.1
* @date 2016年8月23日
*/
@Service("companyProducer")
public class CompanyProducer {
private static Logger logger = LoggerFactory.getLogger(RedisUtils.class);
public static final String ActiveMQQueueKey="Company.ActiveMQ.queue";
public static final String ActiveMQQueueKeyA="Company.ActiveMQ.queueA";
public static final String ActiveMQQueueKeyB="Company.ActiveMQ.queueB";
public static String RUN_MESSAGE="ActvieMQ连接异常,请开启ActvieMQ服务.";
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
@Autowired
private JmsMessagingTemplate jmsTemplate;
// 发送消息,destination是发送到的队列,message是待发送的消息
public void sendMessage( final String message){
try{
Destination destination = new ActiveMQQueue(CompanyProducer.ActiveMQQueueKey);
jmsTemplate.convertAndSend(destination, message);
}catch (Exception e){
logger.error(dateFormat.format(new Date()) + " | " +"ActvieMQ:"+RUN_MESSAGE+e.getMessage(), RUN_MESSAGE+e.getMessage());
}
}
public void sendMessageA( final String message){
try{
Destination destinationA = new ActiveMQQueue(CompanyProducer.ActiveMQQueueKeyA);
jmsTemplate.convertAndSend(destinationA, message);
}catch (Exception e){
logger.error(dateFormat.format(new Date()) + " | " +"ActvieMQ:"+RUN_MESSAGE+e.getMessage(), RUN_MESSAGE+e.getMessage());
}
}
public void sendMessageB(final String message){
try{
Destination destinationB = new ActiveMQQueue(CompanyProducer.ActiveMQQueueKeyA);
jmsTemplate.convertAndSend(destinationB, message);
}catch (Exception e){
logger.error(dateFormat.format(new Date()) + " | " +"ActvieMQ:"+RUN_MESSAGE+e.getMessage(), RUN_MESSAGE+e.getMessage());
}
}
@JmsListener(destination="company.out.queue")
public void consumerMessage(String text){
System.out.println(dateFormat.format(new Date()) + " | " +"ActvieMQ:从out.queue队列收到的回复报文为:"+text);
}
}
\ No newline at end of file
package com.jeespring.modules.activeMQ;
import com.jeespring.common.persistence.AbstractBaseEntity;
/**
* 消息生产者.
* @author 黄炳桂 516821420@qq.com
* @version v.0.1
* @date 2016年8月23日
*/
public class Email extends AbstractBaseEntity<Email> {
private String toMailAddr;
private String subject;
private String message;
public String getToMailAddr() {
return toMailAddr;
}
public void setToMailAddr(String toMailAddr) {
this.toMailAddr = toMailAddr;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
package com.jeespring.modules.activeMQ;
import com.jeespring.common.utils.SendMailUtil;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 消息消费者.
* @author 黄炳桂 516821420@qq.com
* @version v.0.1
* @date 2016年8月23日
*/
@Component
public class JeeSpringConsumer {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = JeeSpringProducer.ActiveMQQueueKeySendMailList)
public void receiveQueue(List<String> list) {
String toMailAddr=list.get(0);
String subject=list.get(1);
String message=list.get(2);
SendMailUtil.sendCommonMail(toMailAddr,subject,message);
}
@JmsListener(destination = JeeSpringProducer.ActiveMQQueueKeySendMailObject)
public void receiveQueue(Email email) {
SendMailUtil.sendCommonMail(email.getToMailAddr(),email.getSubject(),email.getMessage());
}
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = JeeSpringProducer.ActiveMQQueueKey)
public void receiveQueue(String text) {
System.out.println(dateFormat.format(new Date()) + " | " +"ActiveMQ Consumer :"+JeeSpringProducer.ActiveMQQueueKey+":收到的报文为:"+text);
}
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = JeeSpringProducer.ActiveMQQueueKeyA)
public void receiveQueueA(String text) {
System.out.println(dateFormat.format(new Date()) + " | " +"ActiveMQ Consumer :"+JeeSpringProducer.ActiveMQQueueKeyA+":收到的报文为:"+text);
}
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = JeeSpringProducer.ActiveMQQueueKeyB)
@SendTo("jeespring.out.queue")
public String receiveQueueB(String text) {
System.out.println(dateFormat.format(new Date()) + " | " +"ActiveMQ Consumer :"+JeeSpringProducer.ActiveMQQueueKeyA+"收到的报文为:"+text);
return "return message:"+text;
}
}
\ No newline at end of file
package com.jeespring.modules.activeMQ;
import com.jeespring.common.redis.RedisUtils;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import javax.jms.Destination;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 消息生产者.
* @author 黄炳桂 516821420@qq.com
* @version v.0.1
* @date 2016年8月23日
*/
@Service
public class JeeSpringProducer {
private static Logger logger = LoggerFactory.getLogger(RedisUtils.class);
public static final String ActiveMQQueueKeySendMailList="JeeSpring.ActiveMQ.queue.sendmaillist";
public static final String ActiveMQQueueKeySendMailObject="JeeSpring.ActiveMQ.queue.sendmailobject";
public static final String ActiveMQQueueKey="JeeSpring.ActiveMQ.queue";
public static final String ActiveMQQueueKeyA="JeeSpring.ActiveMQ.queueA";
public static final String ActiveMQQueueKeyB="JeeSpring.ActiveMQ.queueB";
public static String RUN_MESSAGE="ActvieMQ连接异常,请开启ActvieMQ服务.";
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
@Autowired
private JmsMessagingTemplate jmsTemplate;
// 发送消息,destination是发送到的队列,message是待发送的消息
public void sendMessage( Destination destination,List<String> list){
try{
jmsTemplate.convertAndSend(destination, list);
}catch (Exception e){
logger.error(dateFormat.format(new Date()) + " | " +"ActvieMQ:"+RUN_MESSAGE+e.getMessage(), RUN_MESSAGE+e.getMessage());
}
}
public void sendMessage( Destination destination,Email email){
try{
jmsTemplate.convertAndSend(destination, email);
}catch (Exception e){
logger.error(dateFormat.format(new Date()) + " | " +"ActvieMQ:"+RUN_MESSAGE+e.getMessage(), RUN_MESSAGE+e.getMessage());
}
}
public void sendMessage( Destination destination,String message){
try{
jmsTemplate.convertAndSend(destination, message);
}catch (Exception e){
logger.error(dateFormat.format(new Date()) + " | " +"ActvieMQ:"+RUN_MESSAGE+e.getMessage(), RUN_MESSAGE+e.getMessage());
}
}
public void sendMessageA( final String message){
try{
Destination destinationA = new ActiveMQQueue(JeeSpringProducer.ActiveMQQueueKeyA);
jmsTemplate.convertAndSend(destinationA, message);
}catch (Exception e){
logger.error(dateFormat.format(new Date()) + " | " +"ActvieMQ:"+RUN_MESSAGE+e.getMessage(), RUN_MESSAGE+e.getMessage());
}
}
public void sendMessageB(final String message){
try{
Destination destinationB = new ActiveMQQueue(JeeSpringProducer.ActiveMQQueueKeyA);
jmsTemplate.convertAndSend(destinationB, message);
}catch (Exception e){
logger.error(dateFormat.format(new Date()) + " | " +"ActvieMQ:"+RUN_MESSAGE+e.getMessage(), RUN_MESSAGE+e.getMessage());
}
}
@JmsListener(destination="jeespring.out.queue")
public void consumerMessage(String text){
System.out.println(dateFormat.format(new Date()) + " | " +"ActvieMQ:从out.queue队列收到的回复报文为:"+text);
}
}
\ No newline at end of file
package com.jeespring.modules.activeMQ;
import com.jeespring.common.utils.SendMailUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.jms.Destination;
import java.util.ArrayList;
import java.util.List;
/**
* ActiveMQController
* @author 黄炳桂 516821420@qq.com
* @version 2014-05-16
*/
@RestController
@RequestMapping(value = "rest/mq/producer")
@Api(value="ActiveMQ队列任务云接口", description="ActiveMQ队列任务云接口")
public class JeeSpringProducerRestController {
@Autowired
private JeeSpringProducer jeeSpringProducer;
@RequestMapping(value = {"sendMessage"},method ={RequestMethod.POST,RequestMethod.GET})
@ApiOperation(value="ActiveMQ队列云发送信息(Content-Type为text/html)", notes="ActiveMQ队列云发送信息(Content-Type为text/html)")
@ApiImplicitParam(name = "message", value = "信息", required = false, dataType = "String",paramType="query")
public void sendMessage(@RequestParam(required=false) String message){
Destination destination = new ActiveMQQueue(JeeSpringProducer.ActiveMQQueueKey);
jeeSpringProducer.sendMessage(destination,message);
}
@RequestMapping(value = {"sendMail"},method ={RequestMethod.POST,RequestMethod.GET})
@ApiOperation(value="ActiveMQ队列云发送邮件(Content-Type为text/html)", notes="ActiveMQ队列云发送邮件(Content-Type为text/html)")
@ApiImplicitParams({
@ApiImplicitParam(name = "toMailAddr", value = "接收邮件", required = false, dataType = "String",paramType="query"),
@ApiImplicitParam(name = "subject", value = "邮件主题", required = false, dataType = "String",paramType="query"),
@ApiImplicitParam(name = "message", value = "邮件内容", required = false, dataType = "String",paramType="query")
})
public void sendMail(@RequestParam(required=false) String toMailAddr, @RequestParam(required=false) String subject, @RequestParam(required=false) String message) {
List<String> list=new ArrayList();
list.add(toMailAddr);
list.add(subject);
list.add(message);
Destination destination = new ActiveMQQueue(JeeSpringProducer.ActiveMQQueueKeySendMailList);
jeeSpringProducer.sendMessage(destination,list);
}
@RequestMapping(value = {"sendMailObject"},method ={RequestMethod.POST,RequestMethod.GET})
@ApiOperation(value="ActiveMQ队列云发送邮件(Content-Type为text/html)", notes="ActiveMQ队列云发送邮件(Content-Type为text/html)")
@ApiImplicitParam(name = "email", value = "email信息{toMailAddr,subject,message}", required = false, dataType = "Email",paramType="query")
public void sendMailObject(Email email) {
Destination destination = new ActiveMQQueue(JeeSpringProducer.ActiveMQQueueKeySendMailObject);
jeeSpringProducer.sendMessage(destination,email);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment