01: package demo.mtclient;
02:
03: /**
04: *
05: * Test multi-threading and call-back support:
06: *
07: * use any number of ClientThreads on the same server
08: * object.
09: */
10:
11: import org.omg.CosNaming.*;
12: import org.omg.PortableServer.*;
13: import org.omg.CORBA.*;
14:
15: public class Client {
16: public static void main(String[] args) {
17: MyServer s = null;
18:
19: try {
20: int clientNum = 2;
21:
22: if (args.length > 0)
23: clientNum = Integer.parseInt(args[0]);
24:
25: String msg = "<test_msg>";
26: /* Make sure that you allow a maximum thread
27: * pool size > 1, otherwise this will block.
28: */
29: java.util.Properties props = new java.util.Properties();
30: props.put("jacorb.poa.thread_pool_max", Integer
31: .toString(clientNum * 2));
32:
33: ORB orb = ORB.init(args, props);
34:
35: // get hold of the naming service
36: NamingContextExt nc = NamingContextExtHelper.narrow(orb
37: .resolve_initial_references("NameService"));
38:
39: s = MyServerHelper.narrow(nc.resolve(nc
40: .to_name("Thread.example")));
41:
42: POA poa = POAHelper.narrow(orb
43: .resolve_initial_references("RootPOA"));
44:
45: poa.the_POAManager().activate();
46:
47: /* create thread objects */
48: ClientThread[] clientThread = new ClientThread[clientNum];
49: for (int i = 0; i < clientNum; i++) {
50: clientThread[i] = new ClientThread(s, msg, i);
51: }
52:
53: /* create CORBA references for each client thread */
54: Observer[] observers = new Observer[clientNum];
55: for (int i = 0; i < clientNum; i++) {
56: observers[i] = ObserverHelper.narrow(poa
57: .servant_to_reference(new ObserverPOATie(
58: clientThread[i])));
59: clientThread[i].setMe(observers[i]);
60: }
61:
62: /* start threads */
63:
64: for (int i = 0; i < clientNum; i++) {
65: clientThread[i].start();
66: }
67:
68: int which = 0;
69: while (which < clientNum) {
70: while (clientThread[which].isAlive())
71: Thread.currentThread().sleep(500);
72: which++;
73: }
74:
75: System.out.println("Going down...");
76:
77: orb.shutdown(true);
78:
79: } catch (Exception e) {
80: e.printStackTrace();
81: }
82:
83: }
84: }
|