KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > tutorial > interceptor > bean > EmailSystemBean


1 /*
2  * JBoss, the OpenSource EJB server
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.tutorial.interceptor.bean;
8
9 import javax.ejb.AroundInvoke;
10 import javax.ejb.Interceptors;
11 import javax.ejb.InvocationContext;
12 import javax.ejb.Stateless JavaDoc;
13 import javax.ejb.Remote JavaDoc;
14 import javax.jms.Queue JavaDoc;
15 import javax.jms.QueueConnection JavaDoc;
16 import javax.jms.QueueConnectionFactory JavaDoc;
17 import javax.jms.QueueSender JavaDoc;
18 import javax.jms.QueueSession JavaDoc;
19 import javax.jms.TextMessage JavaDoc;
20 import javax.naming.InitialContext JavaDoc;
21
22 @Stateless JavaDoc
23 @Interceptors ({TracingInterceptor.class, OtherInterceptor.class})
24 @Remote JavaDoc(EmailSystem.class)
25 public class EmailSystemBean implements EmailSystem
26 {
27    public void emailLostPassword(String JavaDoc username)
28    {
29       System.out.println("----------------");
30       System.out.println("In EmailSystemBean business method");
31       System.out.println("----------------");
32       //Pretend we are looking up password and email, and place a message on the queue
33
String JavaDoc password = "xyz";
34       String JavaDoc email = "xyz@lalala.com";
35       sendMessage(username, password, email);
36    }
37
38    @AroundInvoke
39    public Object JavaDoc myBeanInterceptor(InvocationContext ctx) throws Exception JavaDoc
40    {
41       if (ctx.getMethod().getName().equals("emailLostPassword"))
42       {
43          System.out.println("*** EmailSystemBean.myBeanInterceptor - username: " + ctx.getParameters()[0]);
44       }
45
46       return ctx.proceed();
47    }
48
49    private void sendMessage(String JavaDoc username, String JavaDoc pwd, String JavaDoc email)
50    {
51       try
52       {
53          QueueConnection JavaDoc cnn = null;
54          QueueSender JavaDoc sender = null;
55          QueueSession JavaDoc session = null;
56          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
57          Queue JavaDoc queue = (Queue JavaDoc) ctx.lookup("queue/tutorial/example");
58          QueueConnectionFactory JavaDoc factory = (QueueConnectionFactory JavaDoc) ctx.lookup("ConnectionFactory");
59          cnn = factory.createQueueConnection();
60          session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
61
62          TextMessage JavaDoc msg = session.createTextMessage(username + ":" + pwd + ":" + email);
63
64          sender = session.createSender(queue);
65          sender.send(msg);
66          System.out.println("Message sent successfully to remote queue.");
67       }
68       catch(Exception JavaDoc e)
69       {
70          throw new RuntimeException JavaDoc(e);
71       }
72    }
73 }
74
Popular Tags