1 22 package org.jboss.ejb3.test.appclient.client; 23 24 import javax.annotation.Resource; 25 import javax.ejb.EJB ; 26 import javax.jms.Connection ; 27 import javax.jms.ConnectionFactory ; 28 import javax.jms.Destination ; 29 import javax.jms.MessageConsumer ; 30 import javax.jms.MessageProducer ; 31 import javax.jms.Queue ; 32 import javax.jms.Session ; 33 import javax.jms.TextMessage ; 34 35 import org.jboss.ejb3.test.appclient.HelloWorldService; 36 37 43 public class HelloWorldClient 44 { 45 @EJB 46 private static HelloWorldService helloWorldService; 47 48 @Resource(name="msg") 49 private static String msg; 50 51 private static String result; 52 53 @Resource(mappedName="ConnectionFactory") 54 private static ConnectionFactory connectionFactory; 55 56 @Resource(name="messageReplier") 57 private static Destination destination; 58 59 public static String getResult() 60 { 61 return result; 62 } 63 64 public static void main(String args[]) 65 { 66 String name = "unspecified"; 67 if(args.length > 0) 68 name = args[0]; 69 70 if(helloWorldService == null) 71 throw new NullPointerException ("helloWorldService is null"); 72 73 if(msg == null) 74 throw new NullPointerException ("msg is null"); 75 76 result = helloWorldService.sayHelloTo(name) + ", " + msg; 77 78 testMDB(); 79 } 80 81 public static void testMDB() 82 { 83 if(connectionFactory == null) 84 throw new NullPointerException ("connectionFactory is null"); 85 86 if(destination == null) 87 throw new NullPointerException ("destination is null"); 88 89 try 90 { 91 Connection conn = connectionFactory.createConnection(); 92 Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); 93 94 Queue replyTo = session.createTemporaryQueue(); 95 TextMessage msg = session.createTextMessage("Hello world"); 96 msg.setJMSReplyTo(replyTo); 97 98 MessageConsumer consumer = session.createConsumer(replyTo); 99 conn.start(); 100 101 MessageProducer producer = session.createProducer(destination); 102 producer.send(destination, msg); 103 104 TextMessage reply = (TextMessage ) consumer.receive(2000); 105 System.out.println("reply = " + reply.getText()); 106 107 producer.close(); 108 conn.stop(); 109 consumer.close(); 110 session.close(); 111 } 112 catch(Exception e) 113 { 114 throw new RuntimeException (e); 115 } 116 } 117 } 118 | Popular Tags |