ActivityMQ集成到springmvc中

生产者

pom.xml配置文件

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  4.0.0
  activemq
  activemq-provider
  war
  0.0.1-SNAPSHOT
  activemq-provider Maven Webapp
  http://maven.apache.org
   
       
           
              true  
         
 
          public  
          Public Repositories  
          http://localhost:8081/nexus/content/groups/public/  
     
 
 
 
   
       
          public  
          Public Repositories  
          http://localhost:8081/nexus/content/groups/public/  
     
 
 
 
 
 
    UTF-8
    4.11
    3.2.0.RELEASE
    4.3.1
 
 
 

org.springframework
spring-test
${spring.version}
test 


org.springframework
spring-webmvc
${spring.version}


org.springframework
spring-core
${spring.version}


org.springframework
spring-context
${spring.version}


org.springframework
spring-context-support
${spring.version}


org.springframework
spring-jms
${spring.version}


org.springframework
spring-oxm
${spring.version}


org.mybatis
mybatis
3.2.8


org.mybatis
mybatis-spring
1.1.1
 

org.apache.activemq
activemq-all
5.11.1


org.apache.activemq
activemq-pool
5.11.1


com.alibaba
druid
0.2.9


org.aspectj
aspectjweaver
1.7.1


org.codehaus.jackson
jackson-mapper-asl
1.9.11


commons-fileupload
commons-fileupload
1.2.2


org.apache.commons
commons-lang3
3.3.1


commons-io
commons-io
2.4


org.apache.httpcomponents
httpclient-cache
${httpclient.version}


org.apache.httpcomponents
httpclient
${httpclient.version}


org.apache.httpcomponents
httpcore
${httpclient.version}


com.alibaba
fastjson
1.1.26
 

org.slf4j
slf4j-api
1.7.9


org.slf4j
jul-to-slf4j
1.7.6


org.slf4j
jcl-over-slf4j
1.7.6


javax.servlet
jstl
1.2


javax.servlet
servlet-api
3.0-alpha-1
provided

 
        com.github.miemiedev  
        mybatis-paginator  
        1.2.10  
       

   
com.ojdbc7
ojdbc7
1.0


mysql
mysql-connector-java
5.1.21


org.jboss.marshalling
jboss-marshalling
1.3.0.CR9
 
   
org.jboss.marshalling
jboss-marshalling-serial
1.3.0.CR9


junit
junit
${junit.version}
test

 
org.apache.curator
curator-framework
2.4.2
 
 
org.apache.curator
curator-recipes
2.4.2

 

 
    activemq-provider
 


log4j.properties配置文件


log4j.rootLogger=INFO, console, file
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.file.File=D:/002_developer/workspace_001/zcmoni.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.logger.org.springframework=WARN



config.properties配置文件


## ActiveMQ Config
activemq.brokerURL=tcp\://192.168.1.200\:61616
activemq.userName=bhz
activemq.password=bhz
activemq.pool.maxConnections=10
#queueName

activemq.queueName=mailqueue


spring-context.xml配置




xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
           http://www.springframework.org/schema/aop   
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
           http://www.springframework.org/schema/tx  
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-3.2.xsd"
default-autowire="byName" default-lazy-init="false">








spring-activemq.xml配置文件




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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
           http://www.springframework.org/schema/aop   
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
           http://www.springframework.org/schema/tx  
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-3.2.xsd"

default-autowire="byName" default-lazy-init="false">





       
       
         

   













 
     
     
   

 




web.xml配置文件




  activemq-provider
 
    index.jsp
 
 
    webAppRootKey
    activemq-provider.root
 
 
    log4jConfigLocation
    /WEB-INF/classes/log4j.properties
 
 
    contextConfigLocation
    classpath*:spring-context.xml
 
 
    org.springframework.web.util.Log4jConfigListener
 
 
    org.springframework.web.context.ContextLoaderListener
   
 
   15

 



实体类


package bhz.entity;
public class Mail {
/** 发件人 **/
private String from;
/** 收件人 **/
private String to;
/** 主题 **/
private String subject;
/** 邮件内容 **/
private String content;
public Mail(){}
public Mail(String from, String to, String subject, String content) {
super();
this.from = from;
this.to = to;
this.subject = subject;
this.content = content;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}


业务逻辑类:


import com.alibaba.fastjson.JSONObject;


/**
 * 系统名称:

 * 模块名称:

 * 中文类名:

 * 概要说明:

 * @author bhz(Alienware)
 * @since 2014年7月2日
 */
@Service("mqProducer")
public class MQProducer {
private JmsTemplate jmsTemplate;
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
@Autowired
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
/**
* 方法名称:发送邮件信息对象

* 概要说明:发送邮件信息对象

* @param mail
*/
public void sendMessage(final Mail mail) {
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(JSONObject.toJSONString(mail));
}
});
}


}


测试类:


import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import bhz.entity.Mail;
import bhz.mq.MQProducer;
/**
 * 系统名称:

 * 模块名称:

 * 中文类名:

 * 概要说明:

 * @author bhz(Alienware)
 * @since 2014年7月2日
 */
@ContextConfiguration(locations = {"classpath:spring-context.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class TestProducer {
@Autowired
private MQProducer mqProducer;
@Test
public void send(){
Mail mail = new Mail();
mail.setTo("[email protected]");
mail.setSubject("异步发送邮件");
mail.setContent("Hi,This is a message!");
this.mqProducer.sendMessage(mail);
System.out.println("发送成功..");

}


}






消费者

pox.xml配置文件



  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  4.0.0
  activemq
  activemq-consumer
  war
  0.0.1-SNAPSHOT
  activemq-consumer Maven Webapp
  http://maven.apache.org
  
   
       
           
              true  
           
          public  
          Public Repositories  
          http://localhost:8081/nexus/content/groups/public/  
       
   
   
       
          public  
          Public Repositories  
          http://localhost:8081/nexus/content/groups/public/  
       
   




 
 
    UTF-8
    4.11
    3.2.0.RELEASE
    4.3.1
   
  
  
 

org.springframework
spring-test
${spring.version}
test 


org.springframework
spring-webmvc
${spring.version}


org.springframework
spring-core
${spring.version}


org.springframework
spring-context
${spring.version}


org.springframework
spring-context-support
${spring.version}


org.springframework
spring-jms
${spring.version}


org.springframework
spring-oxm
${spring.version}


org.mybatis
mybatis
3.2.8


org.mybatis
mybatis-spring
1.1.1
 





org.apache.activemq
activemq-all
5.11.1


org.apache.activemq
activemq-pool
5.11.1


com.alibaba
druid
0.2.9


org.aspectj
aspectjweaver
1.7.1


org.codehaus.jackson
jackson-mapper-asl
1.9.11


commons-fileupload
commons-fileupload
1.2.2


org.apache.commons
commons-lang3
3.3.1


commons-io
commons-io
2.4


org.apache.httpcomponents
httpclient-cache
${httpclient.version}


org.apache.httpcomponents
httpclient
${httpclient.version}


org.apache.httpcomponents
httpcore
${httpclient.version}


com.alibaba
fastjson
1.1.26
 

org.slf4j
slf4j-api
1.7.9


org.slf4j
jul-to-slf4j
1.7.6


org.slf4j
jcl-over-slf4j
1.7.6


javax.servlet
jstl
1.2


javax.servlet
servlet-api
3.0-alpha-1
provided

 
        com.github.miemiedev  
        mybatis-paginator  
        1.2.10  
       
   
com.ojdbc7
ojdbc7
1.0


mysql
mysql-connector-java
5.1.21


org.jboss.marshalling
jboss-marshalling
1.3.0.CR9
 
   
org.jboss.marshalling
jboss-marshalling-serial
1.3.0.CR9


junit
junit
${junit.version}
test

 
org.apache.curator
curator-framework
2.4.2
 
 
org.apache.curator
curator-recipes
2.4.2





 
    
 
    activemq-consumer
 



config.properties配置文件


## ActiveMQ Configuration
activemq.brokerURL=tcp\://192.168.1.200\:61616
activemq.userName=bhz
activemq.password=bhz
activemq.pool.maxConnections=10
#queueName
activemq.queueName=mailqueue


## SMTP Configuration
mail.host=smtp.163.com
##mail.port=21
mail.username=***@163.com
mail.password=
mail.smtp.auth=true
mail.smtp.timeout=30000
mail.default.from=***@163.com


log4j.properties配置文件


log4j.rootLogger=INFO, console, file
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.file.File=D:/002_developer/workspace_001/zcmoni.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.logger.org.springframework=WARN


spring-context.xml配置文件




xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
           http://www.springframework.org/schema/aop   
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
           http://www.springframework.org/schema/tx  
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-3.2.xsd"
default-autowire="byName" default-lazy-init="false">






















spring-activemq.xml配置文件




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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
           http://www.springframework.org/schema/aop   
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd  
           http://www.springframework.org/schema/tx  
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd  
           http://www.springframework.org/schema/context  
           http://www.springframework.org/schema/context/spring-context-3.2.xsd"
default-autowire="byName" default-lazy-init="false">







       
       
         


   

















 
     
     
   
 




${activemq.queueName}























spring-mail.xml配置文件




xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
       http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">
       









${mail.smtp.auth}
${mail.smtp.timeout}







${mail.default.from}



















web.xml配置和上面的web.xml基本类似






实体类


public class Mail {


/** 发件人 **/
private String from;
/** 收件人 **/
private String to;
/** 主题 **/
private String subject;
/** 邮件内容 **/
private String content;

public Mail(){}

public Mail(String from, String to, String subject, String content) {
super();
this.from = from;
this.to = to;
this.subject = subject;
this.content = content;
}

public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}




}




监听器:


/**
 * 系统名称:

 * 模块名称:

 * 中文类名:

 * 概要说明:

 * @author bhz(Alienware)
 * @since 2014年7月2日
 */
@Component
public class MailQueueMessageListener implements SessionAwareMessageListener {


@Autowired
private JmsTemplate jmsTemplate;
@Autowired
private Destination mailQueue;
@Autowired
private MailService mailService;


public synchronized void onMessage(Message message, Session session) {
try {
TextMessage msg = (TextMessage) message;
final String ms = msg.getText();
System.out.println("收到消息:" + ms);
//转换成相应的对象
Mail mail = JSONObject.parseObject(ms, Mail.class);
if (mail == null) {
return;
}
try {
//执行发送业务
mailService.mailSend(mail);

} catch (Exception e) {
// 发送异常,重新放回队列
// jmsTemplate.send(mailQueue, new MessageCreator() {
// @Override
// public Message createMessage(Session session) throws JMSException {
// return session.createTextMessage(ms);
// }
// });
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}


业务逻辑类


import bhz.entity.Mail;


@Service("mailService")
public class MailService {


@Autowired
private JavaMailSender mailSender;
@Autowired
private SimpleMailMessage simpleMailMessage;
@Autowired
private ThreadPoolTaskExecutor threadPool;


/**
* 方法名称:发送邮件

* 概要说明:发送邮件

* @param mail
*/
public void mailSend(final Mail mail) {
threadPool.execute(new Runnable() {
public void run() {
try {
simpleMailMessage.setFrom(simpleMailMessage.getFrom()); 
simpleMailMessage.setTo(mail.getTo()); 
simpleMailMessage.setSubject(mail.getSubject());
simpleMailMessage.setText(mail.getContent());
mailSender.send(simpleMailMessage);
} catch (MailException e) {
e.printStackTrace();
throw e;
}
}
});
}
}




测试类:


import org.springframework.context.support.ClassPathXmlApplicationContext;




/**
 * 系统名称:

 * 模块名称:

 * 中文类名:

 * 概要说明:

 * @author bhz(Alienware)
 * @since 2014年7月2日
 */
public class TestConsumer {




public static void main(String[] args) {
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "spring-context.xml" });
context.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}




你可能感兴趣的:(ActivityMQ,spring环境配置)