1 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 ; 13 import javax.ejb.Remote ; 14 import javax.jms.Queue ; 15 import javax.jms.QueueConnection ; 16 import javax.jms.QueueConnectionFactory ; 17 import javax.jms.QueueSender ; 18 import javax.jms.QueueSession ; 19 import javax.jms.TextMessage ; 20 import javax.naming.InitialContext ; 21 22 @Stateless 23 @Interceptors ({TracingInterceptor.class, OtherInterceptor.class}) 24 @Remote (EmailSystem.class) 25 public class EmailSystemBean implements EmailSystem 26 { 27 public void emailLostPassword(String username) 28 { 29 System.out.println("----------------"); 30 System.out.println("In EmailSystemBean business method"); 31 System.out.println("----------------"); 32 String password = "xyz"; 34 String email = "xyz@lalala.com"; 35 sendMessage(username, password, email); 36 } 37 38 @AroundInvoke 39 public Object myBeanInterceptor(InvocationContext ctx) throws Exception 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 username, String pwd, String email) 50 { 51 try 52 { 53 QueueConnection cnn = null; 54 QueueSender sender = null; 55 QueueSession session = null; 56 InitialContext ctx = new InitialContext (); 57 Queue queue = (Queue ) ctx.lookup("queue/tutorial/example"); 58 QueueConnectionFactory factory = (QueueConnectionFactory ) ctx.lookup("ConnectionFactory"); 59 cnn = factory.createQueueConnection(); 60 session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); 61 62 TextMessage 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 e) 69 { 70 throw new RuntimeException (e); 71 } 72 } 73 } 74 | Popular Tags |