环境:jdk1.6/jboss4.2
前提:一些基本的EJB知识
1. 在jboss-4.2.2.GA\server\default\deploy目录下,新建simple-jms-service.xml文件,定义消息存放地址,
<?xml version="1.0" encoding="UTF-8"?>
<server>
<mbean code="org.jboss.mq.server.jmx.Queue"
name="jboss.mq.destination:service=Queue,name=simplejms">
<attribute name="JNDIName">queue/simplejms</attribute>
<depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
</mbean>
</server>
部署成功后在jmx-console页面上可以看到刚部署的queue/simplejms。
2. 修改同一目录下mail-service.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: mail-service.xml 62349 2007-04-15 16:48:15Z dimitris@jboss.org $ -->
<server>
<!-- ==================================================================== -->
<!-- Mail Connection Factory -->
<!-- ==================================================================== -->
<mbean code="org.jboss.mail.MailService"
name="jboss:service=Mail">
<attribute name="JNDIName">java:/Mail</attribute>
<attribute name="User">你邮件用户名</attribute>
<attribute name="Password">邮件密码</attribute>
<attribute name="Configuration">
<!-- A test configuration -->
<configuration>
<!-- Change to your mail server prototocol -->
<property name="mail.store.protocol" value="pop3"/>
<property name="mail.transport.protocol" value="smtp"/>
<!-- Change to the user who will receive mail -->
<property name="mail.user" value="nobody"/>
<!-- Change to the mail server -->
<property name="mail.pop3.host" value="pop3.163.com"/>
<!-- Change to the SMTP gateway server -->
<property name="mail.smtp.host" value="smtp.163.com"/>
<!-- Add to authorize -->
<property name="mail.smtp.auth" value="true"/>
<!-- The mail server port -->
<property name="mail.smtp.port" value="25"/>
<!-- Change to the address mail will be from -->
<property name="mail.from" value="nobody@163.com"/>
<!-- Enable debugging output from the javamail classes -->
<property name="mail.debug" value="false"/>
</configuration>
</attribute>
<depends>jboss:service=Naming</depends>
</mbean>
</server>
3. 编写消息消费者(一个MDB),不是很严密,没有验证为空什么的。
package com.xxx.ejb3.mdb;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
@MessageDriven(activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/simplejms"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto_acknowledge") })
public class NetworkMessageBean implements MessageListener {
@Override
public void onMessage(Message msg) {
try {
if (msg instanceof MapMessage) {
MapMessage mmsg = (MapMessage) msg;
InternetAddress from = new InternetAddress(mmsg
.getString("from"));
InternetAddress to = new InternetAddress(mmsg.getString("to"));
String subject = mmsg.getString("subject");
String content = mmsg.getString("content");
System.out
.println("message is:" + "\nfrom: " + from + "\nto: "
+ to + "\nsuject: " + subject + "\ncontent: "
+ content);
Context ctx = new InitialContext();
Session session = (Session) ctx.lookup("java:/Mail");
javax.mail.Message actualMsg = new MimeMessage(session);
actualMsg.setFrom(from);
actualMsg.setRecipient(javax.mail.Message.RecipientType.TO, to);
actualMsg.setSubject(subject);
actualMsg.setText(content);
Transport.send(actualMsg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
导出成jar包,部署到jboss-4.2.2.GA\server\default\deploy目录。
4. 编写消息生产者/消息发送者,这里使用一个普通的Java Application在main方法中完成消息发送。
package com.xxx.ejb3.client;
import javax.jms.Destination;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.naming.Context;
import com.xxx.ejb3.util.ContextUtil;
public class NetworkMessageProducer {
public static void main(String[] args) {
QueueConnection conn = null;
QueueSession session = null;
try {
Context ctx = ContextUtil.getContext();
QueueConnectionFactory factory = (QueueConnectionFactory) ctx
.lookup("QueueConnectionFactory");
conn = factory.createQueueConnection();
session = conn.createQueueSession(false,
QueueSession.AUTO_ACKNOWLEDGE);
Destination destination = (Queue) ctx.lookup("queue/simplejms");
MessageProducer producer = session.createProducer(destination);
MapMessage msg = session.createMapMessage();
msg.setString("from", "jwu@iteye.com");
msg.setString("to", "jwu@qq.com");
String subject = "no subject/无主题";
String content = "Hi, this is a mail sent by JMS from a piece of code./支持中文吗";
msg.setString("subject", subject);
msg.setString("content", content);
System.out.println("start sending message...");
producer.send(msg);
System.out.println("finish sending message.");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
session.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
在使用这个类时,需要导入jbossall-client.jar和上面自己导出的jar包。
这里使用了一个工具类:ContextUtil.java
package com.xxx.ejb3.util;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class ContextUtil {
private static Context context;
static {
try {
context = new InitialContext();
} catch (NamingException e) {
e.printStackTrace();
}
}
public static Context getContext() {
return context;
}
}
分享到:
相关推荐
jms简单介绍以及在ejb中的使用方法!
JMS JMS Topic(1) 1.消息的生产者 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import javax.jms.JMSException; import javax.jms.ObjectMessage; import javax...
EJB中用JMS模拟多线程机制的设计和实现 作者:高燕 李旭伟 文震 来源:工业技术 / 自动化技术、计算机技术收藏本文章 多线程机制是提高系统执行效率的关键,但对于采用EJB技术的服务器端,由于EJB规范限制使用多线程...
jms-ejb3 source code
本源代码主要向EJB初学者介绍JMS的使用,如何创建和初始化一个请求Requestor对象、如何初始化请求Requestor对象、如何建立一个会话和发送请求、如何打印出请求的过程信息、如何接受由反馈者replier发回的消息等。
EJB持久化规范,EJB核心规范,EJB精简化规范 javaEE5规范 JMS1.1规范 JSP2.1规范 中文版
实战EJB 实战EJB 实战EJB 电子书 pdf
EJB简介 EJB是sun的服务器端组件模型,最大的用处是部署分布式应用程序,类似微软的.net技术。凭借java跨平台的优势,用EJB技术部署的分布式系
EJB方面EJB方面EJB方面EJB方面EJB方面
EJB集群资料EJB集群资料EJB集群资料EJB集群资料
Java中的EJB编程实例代码,内容有:简单的EJB、无状态SessionBean、有状态SessionBean、BMP位图实例、cmp...IIOP、JTA与JTS、JMS的使用、Java IDL、EJB与CORBA的交互、基于EJB的真实世界模型、EJB的商业应用实例等。
内含EJB3持久化规范,EJB3核心规范,JAVAEE+5.0以及JMS1_1规范,希望对大家有帮助
java反射与EJBjava反射与EJBjava反射与EJBjava反射与EJBjava反射与EJBjava反射与EJB
ejb-3_0-fr-spec-ejbcore.pdf ejb-3_0-fr-spec-persistence.pdf ejb-3_0-fr-spec-simplified.pdf
ejb3中文版
ejb+hibernate资料 ejb+hibernate资料 ejb+hibernate资料 ejb+hibernate资料
手动部署EJB 亲自体验EJB开发流程 手动部署EJB 亲自体验EJB开发流程
jms 入门的几个小项目jms 入门的几个小项目jms 入门的几个小项目jms 入门的几个小项目
EJB3.0规范 EJB3.0 SPECIFICATION 官方规范英文版
Files contained in javax.ejb.jar: META-INF/MANIFEST.MF javax.ejb.AccessLocalException.class javax.ejb.AccessTimeout.class javax.ejb.ActivationConfigProperty.class javax.ejb.AfterBegin.class javax....