KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > mtclient > Client


1 package demo.mtclient;
2
3 /**
4  *
5  * Test multi-threading and call-back support:
6  *
7  * use any number of ClientThreads on the same server
8  * object.
9  */

10
11 import org.omg.CosNaming.*;
12 import org.omg.PortableServer.*;
13 import org.omg.CORBA.*;
14
15
16 public class Client
17 {
18     public static void main( String JavaDoc[] args )
19     {
20         MyServer s = null;
21
22         try
23         {
24             int clientNum = 2;
25
26             if( args.length > 0 )
27                 clientNum = Integer.parseInt( args[0] );
28               
29             String JavaDoc msg = "<test_msg>";
30             /* Make sure that you allow a maximum thread
31              * pool size > 1, otherwise this will block.
32              */

33             java.util.Properties JavaDoc props = new java.util.Properties JavaDoc();
34             props.put("jacorb.poa.thread_pool_max", Integer.toString( clientNum * 2 ));
35
36             ORB orb = ORB.init(args, props);
37
38             // get hold of the naming service
39
NamingContextExt nc =
40                 NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));
41
42             s = MyServerHelper.narrow(nc.resolve(nc.to_name("Thread.example")));
43
44             POA poa =
45                 POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
46         
47             poa.the_POAManager().activate();
48         
49             /* create thread objects */
50             ClientThread [] clientThread = new ClientThread [clientNum] ;
51             for( int i = 0; i < clientNum; i++)
52             {
53                 clientThread[i] = new ClientThread(s, msg, i);
54             }
55
56             /* create CORBA references for each client thread */
57             Observer [] observers = new Observer [clientNum];
58             for( int i = 0; i < clientNum; i++)
59             {
60                 observers[i] =
61                     ObserverHelper.narrow(poa.servant_to_reference( new ObserverPOATie( clientThread[i] )));
62                 clientThread[i].setMe( observers[i]);
63             }
64
65             /* start threads */
66
67             for( int i = 0; i < clientNum; i++)
68             {
69                 clientThread[i].start();
70             }
71           
72             int which = 0;
73             while( which < clientNum )
74             {
75                 while( clientThread[which].isAlive() )
76                     Thread.currentThread().sleep(500);
77                 which++;
78             }
79
80             System.out.println("Going down...");
81
82             orb.shutdown(true);
83
84         }
85         catch (Exception JavaDoc e)
86         {
87             e.printStackTrace();
88         }
89
90     }
91 }
92
93
Popular Tags