1 22 package org.jboss.test.invokers.test; 23 24 import java.rmi.RemoteException ; 25 import javax.jms.QueueConnection ; 26 import javax.jms.QueueSession ; 27 import javax.jms.JMSException ; 28 import javax.jms.QueueRequestor ; 29 import javax.jms.Queue ; 30 import javax.jms.Message ; 31 import javax.jms.QueueConnectionFactory ; 32 import javax.jms.ObjectMessage ; 33 import javax.naming.InitialContext ; 34 import javax.naming.NamingException ; 35 36 import org.jboss.proxy.Interceptor; 37 import org.jboss.invocation.Invoker; 38 import org.jboss.invocation.Invocation; 39 import org.jboss.invocation.InvocationContext; 40 import org.jboss.logging.Logger; 41 42 50 public class InvokerInterceptor 51 extends Interceptor 52 { 53 private static Logger log = Logger.getLogger(InvokerInterceptor.class); 54 private transient QueueConnection queConn; 55 private transient QueueSession session; 56 private transient QueueRequestor requestor; 57 58 60 public Object invoke(Invocation invocation) 61 throws Exception 62 { 63 Object returnValue = null; 64 InvocationContext ctx = invocation.getInvocationContext(); 65 String methodName = invocation.getMethod().getName(); 66 if( methodName.equals("doSomethingSlowly") ) 67 { 68 returnValue = sendRecvJMS(invocation); 69 } 70 else 71 { 72 Invoker invoker = ctx.getInvoker(); 74 returnValue = invoker.invoke(invocation); 75 if( methodName.equals("remove") ) 77 { 78 try 79 { 80 if( requestor != null ) 81 requestor.close(); 82 if( session != null ) 83 session.close(); 84 if( queConn != null ) 85 queConn.close(); 86 } 87 catch(Exception e) 88 { 89 log.error("Failed to close jms", e); 90 } 91 } 92 } 93 return returnValue; 94 } 95 96 100 private synchronized Object sendRecvJMS(Invocation invocation) 101 throws RemoteException 102 { 103 Object reply = null; 104 log.info("sendRecvJMS"); 105 try 106 { 107 if( queConn == null ) 108 { 109 InitialContext ctx = null; 110 ctx = new InitialContext (); 111 QueueConnectionFactory qcf = (QueueConnectionFactory ) ctx.lookup("ConnectionFactory"); 112 queConn = qcf.createQueueConnection(); 113 queConn.start(); 114 session = queConn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); 115 Queue queue = (Queue ) ctx.lookup("queue/A"); 116 requestor = new QueueRequestor (session, queue); 117 } 118 Message msg = session.createObjectMessage(invocation.getArguments()); 120 msg.setStringProperty("ejbName", "BusinessSession"); 121 ObjectMessage replyMsg = (ObjectMessage ) requestor.request(msg); 122 reply = replyMsg.getObject(); 123 } 124 catch (NamingException e) 125 { 126 log.error("sendRecvJMS", e); 127 throw new RemoteException ("sendRecvJMS", e); 128 } 129 catch (JMSException e) 130 { 131 log.error("sendRecvJMS", e); 132 throw new RemoteException ("sendRecvJMS", e); 133 } 134 return reply; 135 } 136 } 137 | Popular Tags |