KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > DeadlockTest


1 // $Id: DeadlockTest.java,v 1.6 2004/07/05 14:15:11 belaban Exp $
2

3 package org.jgroups.tests;
4
5
6 import org.jgroups.*;
7 import org.jgroups.blocks.GroupRequest;
8 import org.jgroups.blocks.MethodCall;
9 import org.jgroups.blocks.RpcDispatcher;
10
11
12
13 /**
14  * Class which tests deadlock detection in RequestCorrelator.
15  * @author John Giorgiadis
16  */

17 public class DeadlockTest {
18
19     public class InRpc {
20         public void rpc_1() { _in_rpc_1(); }
21         public void rpc_2() { _in_rpc_2(); }
22     }
23
24
25     private class Handler implements MessageListener, MembershipListener {
26         public Handler() { super(); }
27         // MessageListener
28
public byte[] getState() { return(null); }
29         public void setState(byte[] state) {}
30         public void receive(Message msg) {}
31         // MembershipListener
32
public void block() {}
33         public void suspect(Address suspect) {}
34         public void viewAccepted(View view) {}
35     }
36
37     // .......................................................................
38

39     private String JavaDoc name = "JG";
40     private String JavaDoc stack = null; // default stack config
41
private JChannel channel;
42     private RpcDispatcher disp;
43
44
45     private void _in_rpc_1() {
46         System.out.println("In rpc_1()");
47         cast_call("rpc_2", new Object JavaDoc[]{}, new Class JavaDoc[]{});
48         System.out.println("Exiting rpc_1()");
49     }
50
51     private void _in_rpc_2() {
52         System.out.println("In rpc_2()");
53         System.out.println("Exiting rpc_2()");
54     }
55
56
57     private void cast_call(String JavaDoc method, Object JavaDoc[] args, Class JavaDoc[] types) {
58         MethodCall call;
59         call = new MethodCall(method, args, types);
60         disp.callRemoteMethods(null, call, GroupRequest.GET_ALL, 0);
61     }
62
63     // .......................................................................
64

65     public DeadlockTest(boolean use_deadlock_detection) {
66         Handler handler = new Handler();
67         InRpc in_rpc = new InRpc();
68
69         try {
70             channel = new JChannel(stack);
71             disp = new RpcDispatcher(channel, handler, handler, in_rpc, use_deadlock_detection);
72             channel.connect(name);
73         }
74                 catch(ChannelClosedException ex) { ex.printStackTrace(); }
75                 catch(ChannelException ex) { ex.printStackTrace(); }
76         
77         // Call rpc_1 which in turn calls rpc_2
78
System.out.println("Calling rpc_1()");
79         if(!use_deadlock_detection)
80             System.out.println("** Not using deadlock detection -- recursive call will hang !");
81         else
82             System.out.println("** Using deadlock detection -- recursive call will succeed");
83         cast_call("rpc_1", new Object JavaDoc[]{}, new Class JavaDoc[]{});
84         System.out.println("Out of rpc_1()");
85         channel.disconnect();
86         channel.close();
87         System.out.println("Disconnected");
88     }
89
90     // .......................................................................
91

92     public static void main(String JavaDoc[] args) {
93         if(args.length != 1) {
94         System.out.println("DeadlockTest <true|false (use_deadlock_detection)>");
95         return;
96         }
97         new DeadlockTest(Boolean.valueOf(args[0]).booleanValue());
98     }
99 }
100
Popular Tags