`

ActiveMQ的使用代码样例(异步邮件发送)生成者

阅读更多
public class MailParam {

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

	public MailParam() {
	}

	public MailParam(String to, String subject, String content) {
		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;
	}
}


package com.fsbay.demo.mqtest;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import com.fsbay.demo.mqtest.params.MailParam;

/**
 * 
 * @描述: MQ消息生产者 .
 * @作者: John .
 * @创建时间: 2015-6-17,下午10:55:30 .
 * @版本号: V1.0 .
 */
@Service("mqProducer")
public class MQProducer {
	
	@Autowired
	private JmsTemplate activeMqJmsTemplate;

	/**
	 * 发送消息.
	 * @param mail 
	 */
	public void sendMessage(final MailParam mail) {
		activeMqJmsTemplate.send(new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage(JSONObject.toJSONString(mail));
			}
		});
		
	}

}



<?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: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">

	<!-- 采用注释的方式配置bean -->
	<context:annotation-config />

	<!-- 配置要扫描的包 -->
	<context:component-scan base-package="wusc.edu.demo" />

	<!-- 读入配置属性文件 -->
	<context:property-placeholder location="classpath:mq.properties" />

	<!-- proxy-target-class默认"false",更改为"ture"使用CGLib动态代理 -->
	<aop:aspectj-autoproxy proxy-target-class="true" />	
	
	<import resource="spring-mq.xml" />
</beans>


<?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: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">

	<!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<!-- ActiveMQ服务地址 -->
        <property name="brokerURL" value="${mq.brokerURL}" />
        <property name="userName" value="${mq.userName}"></property>
        <property name="password" value="${mq.password}"></property> 
	</bean>

    <!-- 
    	ActiveMQ为我们提供了一个PooledConnectionFactory,通过往里面注入一个ActiveMQConnectionFactory
    	可以用来将Connection、Session和MessageProducer池化,这样可以大大的减少我们的资源消耗。
    	要依赖于 activemq-pool包
     -->
	<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
		<property name="connectionFactory" ref="targetConnectionFactory" />
		<property name="maxConnections" value="${mq.pool.maxConnections}" />
	</bean>

	<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
	<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
		<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
		<property name="targetConnectionFactory" ref="pooledConnectionFactory" />
	</bean>
	
	<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
	
	<!-- 队列模板 -->
	<bean id="activeMqJmsTemplate" class="org.springframework.jms.core.JmsTemplate">  
	    <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->  
	    <property name="connectionFactory" ref="connectionFactory"/>  
	    <property name="defaultDestinationName" value="${queueName}"></property>
	</bean> 

</beans>


## MQ
mq.brokerURL=tcp\://192.168.4.101\:61616
mq.userName=john
mq.password=john.123
mq.pool.maxConnections=10
#queueName
queueName=com.fsbay.mqtest.v1


<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<parent>
		<groupId>wusc.edu.common</groupId>
		<artifactId>edu-common-parent</artifactId>
		<version>1.0-SNAPSHOT</version>
		<relativePath>../edu-common-parent</relativePath>
	</parent>

	<groupId>com.fsbay.mqtest</groupId>
	<artifactId>edu-demo-mqproducer</artifactId>
	<version>1.0-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>edu-demo-mqproducer</name>
	<url>http://maven.apache.org</url>

	<build>
		<finalName>edu-demo-mqproducer</finalName>
		<resources>
			<resource>
				<targetPath>${project.build.directory}/classes</targetPath>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>**/*.xml</include>
					<include>**/*.properties</include>
				</includes>
			</resource>
		</resources>
	</build>

	<dependencies>

		<!-- Common Dependency Begin -->
		<dependency>
			<groupId>antlr</groupId>
			<artifactId>antlr</artifactId>
		</dependency>
		<dependency>
			<groupId>aopalliance</groupId>
			<artifactId>aopalliance</artifactId>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
		</dependency>
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<classifier>jdk15</classifier>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>ognl</groupId>
			<artifactId>ognl</artifactId>
		</dependency>
		<dependency>
			<groupId>oro</groupId>
			<artifactId>oro</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-digester</groupId>
			<artifactId>commons-digester</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
		</dependency>
		<dependency>
			<groupId>commons-validator</groupId>
			<artifactId>commons-validator</artifactId>
		</dependency>
		<dependency>
			<groupId>dom4j</groupId>
			<artifactId>dom4j</artifactId>
		</dependency>
		<dependency>
			<groupId>net.sf.ezmorph</groupId>
			<artifactId>ezmorph</artifactId>
		</dependency>
		<dependency>
			<groupId>javassist</groupId>
			<artifactId>javassist</artifactId>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
		</dependency>

		<!-- Common Dependency End -->

		<!-- Spring Dependency Begin -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jms</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
		</dependency>

		<!-- Spring Dependency End -->


		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-all</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-pool</artifactId>
		</dependency>


	</dependencies>


</project>


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.fsbay.demo.mqtest.params.MailParam;

/**
 * 
 * @描述: ActiveMQ测试启动类 .
 * @作者: john .
 * @创建时间: 2015-3-17,上午2:25:20 .
 * @版本号: V1.0 .
 */
public class MQProducerTest {
	private static final Log log = LogFactory.getLog(MQProducerTest.class);

	public static void main(String[] args) {
		try {
			ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml");
			context.start();

			MQProducer mqProducer = (MQProducer) context.getBean("mqProducer");
			// 邮件发送
			MailParam mail = new MailParam();
			mail.setTo("sumboy@qq.com");
			mail.setSubject("ActiveMQ测试");
			mail.setContent("通过ActiveMQ异步发送邮件!");

			mqProducer.sendMessage(mail);

			context.stop();
		} catch (Exception e) {
			log.error("==>MQ context start error:", e);
			System.exit(0);
		} finally {
			log.info("===>System.exit");
			System.exit(0);
		}
	}
}

分享到:
评论

相关推荐

    ActiveMQ的使用代码样例(异步邮件发送).rar

    总结来说,这个"ActiveMQ的使用代码样例(异步邮件发送)"提供了如何利用ActiveMQ实现异步通信的实际例子,特别在邮件服务场景中。通过理解这些关键概念和技术,开发者可以有效地构建自己的分布式系统,提高应用的...

    ActiveMQ的使用代码样例(异步邮件发送).zip

    本示例将探讨如何使用ActiveMQ实现异步邮件发送,这有助于提高应用性能,因为邮件发送通常是一个耗时的操作。 首先,我们来看"edu-demo-mqproducer"这个文件,它代表生产者部分,即负责发送消息到ActiveMQ的消息...

    ActiveMq+SpringMVC实现邮件异步发送

    在本项目中,ActiveMQ与SpringMVC框架结合,实现了邮件的异步发送功能。 首先,我们需要理解ActiveMQ的基本概念。ActiveMQ是Apache软件基金会的产品,遵循JMS(Java消息服务)规范,支持多种协议,并且可以跨平台...

    ActiveMQ与Spring整合之异步发送邮件

    2. **异步处理**:通过集成ActiveMQ,可以创建一个消息生产者,将发送邮件的任务作为一个消息放入队列,由后台消费者处理。这样,主流程不会被邮件发送阻塞,提高了系统的响应速度。 3. **消费者端**:在消费者端,...

    JMS+ActiveMQ 完整样例代码

    通过深入研究这个样例代码,初学者可以掌握JMS和ActiveMQ的基本用法,并能够将这些知识应用到自己的项目中,构建可靠的异步通信系统。同时,这也有助于理解和掌握分布式系统中的消息传递机制和设计模式。

    activemq测试样例

    这个“activemq测试样例”压缩包提供了有关如何使用ActiveMQ创建队列、创建消费者以及处理消息积压的示例。通过这个样例,我们可以深入了解ActiveMQ的基本操作和特性。 首先,我们要理解ActiveMQ中的队列。在JMS中...

    JMS+ActiveMQ+Spring 完整样例代码

    在这个"JMS+ActiveMQ+Spring 完整样例代码"中,我们将会探讨如何将这三者结合起来,实现一个简单的消息传递系统。以下是关键的知识点: 1. **JMS接口** JMS定义了两种主要的消息模型:点对点(Point-to-Point,P2P...

    SpringBoot整合ActiveMQ(消息中间件)实现邮件发送功能

    在本项目中,"SpringBoot整合ActiveMQ(消息中间件)实现邮件发送功能"是一个典型的企业级应用示例,它展示了如何将SpringBoot框架与Apache ActiveMQ集成,以实现基于消息队列的邮件发送服务。下面我们将详细探讨这个...

    ActiveMQ实例---分布式发送邮件

    为了实现邮件发送,开发者需要配置ActiveMQ服务器,创建相应的主题或队列,并在生产者和消费者代码中建立与ActiveMQ的连接。此外,还需要正确配置邮件服务的相关参数,如SMTP服务器地址、端口、用户名、密码等。 ...

    基于Java消息服务的异步邮件发送技术.pdf

    【基于Java消息服务的异步邮件发送技术】 随着信息技术的发展,电子邮件已经成为企业内外沟通的关键工具。在复杂的业务环境中,为了确保系统的高效运行,邮件发送必须能够异步进行,避免因邮件处理过程影响到核心...

    activemq生产者和消费者案例代码.zip

    本案例代码包含了一个基本的ActiveMQ生产者和消费者的应用示例,帮助开发者理解如何使用ActiveMQ进行消息传递。 1. **JMS(Java Message Service)简介** JMS是Java平台上的一个标准API,它定义了生产、发送、接收...

    SpringBoot+ActiveMq+MQTT实现消息的发送和接收

    在本文中,我们将深入探讨如何使用SpringBoot、ActiveMQ和MQTT来实现消息的发送与接收。这是一个典型的分布式系统中的消息通信场景,其中SpringBoot作为应用程序框架,ActiveMQ作为消息中间件,而MQTT(Message ...

    ActiveMQ学习笔记之九--发送消息到队列中

    在IT行业中,Apache ActiveMQ是一个广泛使用的开源消息代理和队列服务器,它是Java Message Service (JMS) 的实现,能够处理各种消息传递模式,包括点对点和发布/订阅。这篇"ActiveMQ学习笔记之九--发送消息到队列中...

    ActiveMQ代码用例

    以下是一个简单的ActiveMQ代码用例,展示如何创建连接、发送和接收消息: ```java import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.MessageProducer; import javax.jms.Queue;...

    JMS ActiveMQ演示代码

    总之,这个"JMS ActiveMQ演示代码"提供了学习和实践JMS与ActiveMQ集成的起点,通过分析和运行这个示例,你可以深入理解消息中间件在分布式系统中的作用,以及如何利用JMS和ActiveMQ实现高效、可靠的异步通信。

    ActiveMQ使用手册(中文版)

    ### ActiveMQ 使用手册知识点概述 #### 一、ActiveMQ 原理与基本构件 **1.1 连接工厂(Connection Factory):** - **定义:** 连接工厂是客户端用来创建连接的对象。在ActiveMQ中,`ActiveMQConnectionFactory` 类...

    人工智能-项目实践-异步调度-ActiveMQ+Celery异步任务调度,Python AI.zip

    在Python中,我们可以使用stomp.py库与ActiveMQ进行交互,发送和接收消息。ActiveMQ提供了高可用性、可扩展性和持久化等功能,确保了任务的可靠传递。 **Celery** 是一个分布式任务队列,专注于实时操作,但也支持...

    ActiveMQ实例,源代码

    在提供的源代码实例中,我们可以深入研究 ActiveMQ 的 API 使用、消息生产与消费的实现、队列和主题的配置等。这将有助于我们理解 ActiveMQ 内部的工作机制,如何优化消息传递性能,以及如何处理异常情况。 - **...

    activemq 发送,接受,监控样例程序

    activemq 发送,接受,监控样例程序

    spring使用activeMQ实现消息发送

    本文将深入探讨如何在Spring环境中使用ActiveMQ来实现消息的发送与接收,以提高系统的可扩展性和解耦性。 首先,我们需要了解Spring对ActiveMQ的支持。Spring提供了`spring-jms`模块,它包含了一组丰富的API和配置...

Global site tag (gtag.js) - Google Analytics