1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package uut.service;
22  
23  import java.rmi.RemoteException;
24  import java.util.*;
25  import javax.ejb.CreateException;
26  import javax.ejb.RemoveException;
27  import javax.naming.InitialContext;
28  import javax.naming.Context;
29  import javax.naming.NamingException;
30  
31  import org.apache.log4j.Logger;
32  
33  import uut.ejb.*;
34  
35  import java.io.BufferedReader;
36  import java.io.InputStreamReader;
37  
38  
39  /***
40   * Make tree threads to connect to Weblogic and run them forever.
41   *
42   * Created on 11.12.2004
43   * Contributing Author(s):
44   *
45   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
46   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
47   *
48   * @author vlads
49   * @version $Revision: 1.3 $ ($Author: vlads $)  $Date: 2004/12/13 05:24:54 $
50   */
51  public class ServiceEJBTestCase {
52  
53      protected static final Logger log = Logger.getLogger(ServiceEJBTestCase.class);
54      
55      public static void main(String[] args)  throws Exception {
56          log.debug("ServiceEJBTestCase.main()");
57          int threads = 3;
58          RunnerThread[] th = new RunnerThread[threads];
59          String [] si = {
60                  "uut.service.ServiceImplementation",
61                  "uut.service.ServiceFoo",
62                  "uut.service.ServiceBar"
63          };
64  
65          System.out.println("Starting " + threads + " threads");  	
66          for (int i = 0; i < threads; i++) {
67              RunnerThread t = new RunnerThread(i * 10 + 200, si[i]);
68              th[i] = t;
69              t.start();
70          }
71  
72          BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
73          System.out.println("Type \"q\" Enter to quit.");
74          while (true) {
75              String s = stdin.readLine();
76              if (s.equalsIgnoreCase("q")) {
77                  System.out.println("Exiting. Kill the application if it does not exit.");
78                  break;
79              }
80          }
81          
82          for (int i = 0; i < th.length; i++) {
83              th[i].finish();
84          }
85          
86          for (int i = 0; i < th.length; i++) {
87              th[i].join();
88          }
89      }
90      
91      public static JourUutSession connectToWeblogic(boolean quiet) {
92          Properties p = new Properties();
93  		p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
94  		p.put(Context.PROVIDER_URL, "t3://localhost:7001");
95  		p.put(Context.SECURITY_PRINCIPAL, "system");
96  		p.put(Context.SECURITY_CREDENTIALS, "12345678");
97  
98          JourUutSession handle = null;
99          try {
100             //InitialContext jndi = new InitialContext(p);
101             InitialContext jndi = new InitialContext();
102             Object home = jndi.lookup(JourUutSessionHome.JNDI_NAME);
103             handle = ((JourUutSessionHome) home).create();
104             jndi.close();
105         } catch (RemoteException e) {
106             if (!quiet) {
107                 log.error("Error RemoteException", e);
108             }
109         } catch (Error e) {
110             if (!quiet) {
111                 log.error("Error", e);
112             }
113         } catch (NamingException e) {
114             if (!quiet) {
115                 log.error("Error NamingException", e);
116             }
117         } catch (CreateException e) {
118             if (!quiet) {
119                 log.error("Error CreateException", e);
120             }
121         }
122         return handle;
123     }
124     
125     public static class RunnerThread extends Thread {
126         
127         long millis;
128         String serviceName;
129         JourUutSession handle;
130         int errorCount = 0;
131         int callCount = 0;
132         int callCountReconnect = 100;
133         
134         boolean interrupted = false;
135         
136         RunnerThread(long millis, String serviceName) {
137             this.millis = millis;
138             this.serviceName = serviceName;
139         }
140         
141         public void getServiceHandle() {
142             if (handle == null) {
143                 log.debug("Connection attempt " + errorCount);
144                 handle = connectToWeblogic((errorCount != 0));
145                 try {
146                     if (handle != null) {
147                         handle.selectService(serviceName);
148                         log.info("Use Service " + serviceName);
149                         errorCount = 0;
150                     } else {
151                         errorCount ++;
152                     }
153                 } catch (RemoteException e1) {
154                     log.error("Error", e1);
155                     closeServiceHandle();
156                 }
157             }            
158         }
159         
160         public void closeServiceHandle() {
161             if (handle != null) {
162                 try {
163                     handle.remove();
164                 } catch (RemoteException e) {
165                     log.error("Error", e);
166                 } catch (RemoveException e) {
167                     log.error("Error", e);
168                 }
169                 handle = null;
170             }
171         }
172         
173         public void doCallService() {
174             
175             getServiceHandle();
176             
177             try {
178                 if (handle != null) {
179                     handle.callService("arg " + callCount);
180                     callCount ++;
181                     if ((callCount % callCountReconnect) == 0) {
182                         log.info("Reconnecting " + serviceName);
183                         closeServiceHandle();
184                     }
185                 }
186             } catch (RemoteException e) {
187                 log.error("Error", e);
188                 closeServiceHandle();
189             }
190         }
191         
192         public void finish() {
193             interrupted = true;
194         }
195         
196         public void run() {
197         	while(true) {
198         	    doCallService();
199         	    try {
200                     Thread.sleep(millis);
201                 } catch (InterruptedException e) {
202                     break;
203                 }
204                 if (interrupted) {
205                     break;
206                 }
207         	}
208         	closeServiceHandle();;
209         }
210     }
211 }