环境: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;
}
}