KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > appclient > client > HelloWorldClient


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2006, Red Hat Middleware LLC, and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.test.appclient.client;
23
24 import javax.annotation.Resource;
25 import javax.ejb.EJB JavaDoc;
26 import javax.jms.Connection JavaDoc;
27 import javax.jms.ConnectionFactory JavaDoc;
28 import javax.jms.Destination JavaDoc;
29 import javax.jms.MessageConsumer JavaDoc;
30 import javax.jms.MessageProducer JavaDoc;
31 import javax.jms.Queue JavaDoc;
32 import javax.jms.Session JavaDoc;
33 import javax.jms.TextMessage JavaDoc;
34
35 import org.jboss.ejb3.test.appclient.HelloWorldService;
36
37 /**
38  * Comment
39  *
40  * @author <a HREF="mailto:carlo.dewolf@jboss.com">Carlo de Wolf</a>
41  * @version $Revision: $
42  */

43 public class HelloWorldClient
44 {
45    @EJB JavaDoc
46    private static HelloWorldService helloWorldService;
47    
48    @Resource(name="msg")
49    private static String JavaDoc msg;
50    
51    private static String JavaDoc result;
52    
53    @Resource(mappedName="ConnectionFactory")
54    private static ConnectionFactory JavaDoc connectionFactory;
55    
56    @Resource(name="messageReplier")
57    private static Destination JavaDoc destination;
58    
59    public static String JavaDoc getResult()
60    {
61       return result;
62    }
63    
64    public static void main(String JavaDoc args[])
65    {
66       String JavaDoc name = "unspecified";
67       if(args.length > 0)
68          name = args[0];
69       
70       if(helloWorldService == null)
71          throw new NullPointerException JavaDoc("helloWorldService is null");
72       
73       if(msg == null)
74          throw new NullPointerException JavaDoc("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 JavaDoc("connectionFactory is null");
85       
86       if(destination == null)
87          throw new NullPointerException JavaDoc("destination is null");
88       
89       try
90       {
91          Connection JavaDoc conn = connectionFactory.createConnection();
92          Session JavaDoc session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
93          
94          Queue JavaDoc replyTo = session.createTemporaryQueue();
95          TextMessage JavaDoc msg = session.createTextMessage("Hello world");
96          msg.setJMSReplyTo(replyTo);
97          
98          MessageConsumer JavaDoc consumer = session.createConsumer(replyTo);
99          conn.start();
100          
101          MessageProducer JavaDoc producer = session.createProducer(destination);
102          producer.send(destination, msg);
103          
104          TextMessage JavaDoc reply = (TextMessage JavaDoc) 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 JavaDoc e)
113       {
114          throw new RuntimeException JavaDoc(e);
115       }
116    }
117 }
118
Popular Tags