开发环境

  • IntelliJ IDEA 14.1.4
  • JDK1.7
  • Jboss4.0
  • EJB2.1

项目结构

项目结构

ejb-jar.xml内容

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd" version="2.1">
    <display-name>EJB-MDB</display-name>
    <enterprise-beans>
        <message-driven>
            <ejb-name>EmailMDB</ejb-name>
            <ejb-class>com.wywon.ejb.mdb.EmailMDBBean</ejb-class>
            <transaction-type>Container</transaction-type>
            <message-destination-type>javax.jms.Queue</message-destination-type>
        </message-driven>
    </enterprise-beans>
</ejb-jar>

jboss.xml内容

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 4.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_4_0.dtd">
<jboss>
    <enterprise-beans>
        <message-driven>
            <ejb-name>EmailMDB</ejb-name>
            <destination-jndi-name>queue/EmailQueue</destination-jndi-name>
        </message-driven>
    </enterprise-beans>
</jboss>

EmailClient.java内容

package com.wywon.ejb.mdb;

import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

/**
 * Created by Admin on 2015/10/6.
 */
public class EmailClient {

    private static String QUEUE_NAME = "queue/EmailQueue";

    private QueueSender sender;
    private QueueSession session;

    public EmailClient(){
        try {
            Context ctx  = getInitialContext();

            QueueConnectionFactory conFactory = (QueueConnectionFactory) ctx.lookup("ConnectionFactory");
            QueueConnection connection = conFactory.createQueueConnection();
            session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue chatQueue = (Queue) ctx.lookup(QUEUE_NAME);
            sender = session.createSender(chatQueue);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private Context getInitialContext() throws NamingException {
        Properties environment = new Properties();

        environment.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
        environment.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
        environment.put(Context.PROVIDER_URL,"jnp://localhost:1099");

        return new InitialContext(environment);
    }
    
    public static void main(String args[]) throws JMSException {
        EmailClient client = new EmailClient();
        Hashtable mail = new Hashtable();
        
        mail.put("to","[email protected]");
        mail.put("from","[email protected]");
        mail.put("subject","no subject");
        mail.put("body","no body");
        
        client.sendmail(mail);

        System.out.println("send mail : " + mail);
    }

    private void sendmail(Hashtable mail) throws JMSException {
        MapMessage message = session.createMapMessage();
        Set keys = mail.keySet();
        Iterator i = keys.iterator();
        while (i.hasNext()){
            String key = (String) i.next();
            String val = (String) mail.get(key);

            message.setString(key,val);
        }
        sender.send(message);
    }
}

EmailHelper.java内容

package com.wywon.ejb.mdb;

import java.util.Map;

/**
 * Created by Admin on 2015/10/6.
 */
public class EmailHelper {
    private EmailHelper(){}

    public static void sendmail(Map mail){
        System.out.println("mail : " + mail);
    }
}

EmailMDBBean.java内容

package com.wywon.ejb.mdb;

import javax.ejb.EJBException;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 * Created by Admin on 2015/10/6.
 */
public class EmailMDBBean implements MessageDrivenBean,MessageListener {
    private MessageDrivenContext context;

    public void setMessageDrivenContext(MessageDrivenContext messageDrivenContext) throws EJBException {
        System.out.println("setMessageDrivenContext called");
        this.context = messageDrivenContext;
    }

    public void ejbCreate(){
        System.out.println("ejbCreate called");
    }

    public void ejbRemove() throws EJBException {
        System.out.println("ejbRemove called");
    }

    public void onMessage(Message message) {
        System.out.println("onMessage called");

        try {
            MapMessage mapMessage = (MapMessage) message;
            Enumeration e = mapMessage.getMapNames();
            Hashtable mail = new Hashtable();

            while (e.hasMoreElements()){
                String key = (String) e.nextElement();
                String val = mapMessage.getString(key);
                mail.put(key,val);
            }

            EmailHelper.sendmail(mail);

        } catch (JMSException e1) {
        }
    }
}

(完)