KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xpetstore > services > mail > ejb > MailerMDB


1 package xpetstore.services.mail.ejb;
2
3 import java.util.Date JavaDoc;
4
5 import javax.activation.DataHandler JavaDoc;
6
7 import javax.ejb.MessageDrivenBean JavaDoc;
8 import javax.ejb.MessageDrivenContext JavaDoc;
9
10 import javax.jms.Message JavaDoc;
11 import javax.jms.MessageListener JavaDoc;
12 import javax.jms.ObjectMessage JavaDoc;
13
14 import javax.mail.Session JavaDoc;
15 import javax.mail.Transport JavaDoc;
16 import javax.mail.internet.InternetAddress JavaDoc;
17 import javax.mail.internet.MimeMessage JavaDoc;
18
19 import javax.naming.InitialContext JavaDoc;
20
21 import xpetstore.services.mail.model.Email;
22
23 import xpetstore.util.Debug;
24 import xpetstore.util.JNDINames;
25
26
27 /**
28  * @author <a HREF="mailto:tchbansi@sourceforge.net">Herve Tchepannou</a>
29  *
30  * @ejb.bean
31  * name="Mailer"
32  * acknowledge-mode="Auto-acknowledge"
33  * destination-type="javax.jms.Queue"
34  * subscription-durability="Durable"
35  * transaction-type="Container"
36  * @ejb.transaction
37  * type="NotSupported"
38  * @ejb.resource-ref
39  * res-ref-name="${jndi.mail.session}"
40  * res-type="javax.mail.Session"
41  * res-auth="Container"
42  * jndi-name="${orion.mail.session}"
43  *
44  * @jboss.destination-jndi-name
45  * name="${jboss.queue.mail}"
46  * @jboss.resource-ref
47  * res-ref-name="${jndi.mail.session}"
48  * jndi-name="${jboss.mail.session}"
49  *
50  * @weblogic.message-driven
51  * destination-jndi-name="${weblogic.queue.mail}"
52  * @weblogic.resource-description
53  * res-ref-name="${jndi.mail.session}"
54  * jndi-name="${weblogic.mail.session}"
55  *
56  * @orion.bean
57  * connection-factory-location="${orion.queue.ConnectionFactory}"
58  * destination-location="${orion.queue.mail}"
59  */

60 public class MailerMDB
61     implements MessageDrivenBean JavaDoc,
62                    MessageListener JavaDoc
63 {
64     //~ Methods ----------------------------------------------------------------
65

66     /**
67      * This method expect a <code>javax.jms.ObjectMessage</code> that
68      * contains a {@link Email} as the message object
69      */

70     public void onMessage( Message JavaDoc recvMsg )
71     {
72         try
73         {
74             Debug.print( "MailerMDB.onMessage(" + recvMsg + ")" );
75
76             ObjectMessage JavaDoc msg = ( ObjectMessage JavaDoc ) recvMsg;
77             Email email = ( Email ) msg.getObject( );
78
79             send( email.getTo( ), email.getSubject( ), email.getBody( ) );
80         }
81         catch ( Exception JavaDoc e )
82         {
83             Debug.print( "Unable to send the email", e );
84         }
85     }
86
87     public void send( String JavaDoc to,
88                       String JavaDoc subject,
89                       String JavaDoc body )
90         throws Exception JavaDoc
91     {
92         Debug.print( "MailerMDB.send(" + to + "," + subject + ",...)" );
93
94         InitialContext JavaDoc ic = new InitialContext JavaDoc( );
95         Session JavaDoc session = ( Session JavaDoc ) ic.lookup( JNDINames.MAIL_SESSION );
96         javax.mail.Message JavaDoc msg = new MimeMessage JavaDoc( session );
97
98         msg.setFrom( );
99         msg.setRecipients( javax.mail.Message.RecipientType.TO, InternetAddress.parse( to, false ) );
100         msg.setSubject( subject );
101
102         msg.setDataHandler( new DataHandler JavaDoc( body, "text/html" ) );
103         msg.setHeader( "X-Mailer", "JavaMailer" );
104         msg.setSentDate( new Date JavaDoc( ) );
105
106         Transport.send( msg );
107     }
108
109     //=============================================
110
// EJB callbacks
111
//=============================================
112
public void ejbCreate( ) {}
113
114     public void setMessageDrivenContext( MessageDrivenContext JavaDoc mdc ) {}
115
116     public void ejbRemove( ) {}
117 }
118
Popular Tags