comparison test/org/dancres/blitz/RenewNotify.java @ 0:3dc0c5604566

Initial checkin of blitz 2.0 fcs - no installer yet.
author Dan Creswell <dan.creswell@gmail.com>
date Sat, 21 Mar 2009 11:00:06 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3dc0c5604566
1 package org.dancres.blitz;
2
3 import java.io.Serializable;
4 import java.rmi.MarshalledObject;
5
6 import net.jini.core.entry.Entry;
7 import net.jini.core.lease.Lease;
8
9 import net.jini.core.event.RemoteEvent;
10 import net.jini.core.event.RemoteEventListener;
11
12 import net.jini.core.transaction.*;
13 import net.jini.core.transaction.server.*;
14
15 import org.dancres.blitz.mangler.*;
16
17 import org.dancres.blitz.lease.SpaceUID;
18
19 import org.dancres.blitz.test.TxnMgr;
20 import org.dancres.blitz.test.TxnGatewayImpl;
21
22 public class RenewNotify {
23 public static void main(String args[]) {
24 try {
25 System.out.println("Start space");
26
27 SpaceImpl mySpace = new SpaceImpl(new TxnGatewayImpl());
28
29 System.out.println("Prepare entry");
30
31 EntryMangler myMangler = new EntryMangler();
32 TestEntry myEntry = new TestEntry();
33 myEntry.init();
34
35 System.out.println("init'd entry");
36 MangledEntry myPackedEntry = myMangler.mangle(new TestEntry());
37
38 System.out.println("Do notify");
39
40 RegTicket myTicket =
41 mySpace.notify(myPackedEntry, null, new EventListener(),
42 30000,
43 new MarshalledObject(new Integer(12345)));
44
45 SpaceUID myNotifyUID = myTicket.getUID();
46
47 new Renewer(mySpace, myNotifyUID).start();
48
49 while (true) {
50 System.out.println("Do write");
51
52
53 SpaceUID myEntryUID =
54 mySpace.write(myPackedEntry, null, 50000).getUID();
55
56 if (mySpace.take(myPackedEntry, null, 50000) == null)
57 throw new Exception("Failed to take");
58
59 Thread.sleep(10000);
60 }
61
62 } catch (Exception anE) {
63 System.err.println("Got exception :(");
64 anE.printStackTrace(System.err);
65 }
66 }
67
68 public static class TestEntry implements Entry {
69 public String rhubarb;
70 public Integer count;
71
72 public TestEntry() {
73 }
74
75 public void init() {
76 rhubarb = "blah";
77 count = new Integer(5);
78 }
79
80 public String toString() {
81 return super.toString() + ", " + rhubarb + ", " + count;
82 }
83 }
84
85 private static class EventListener implements RemoteEventListener,
86 Serializable {
87 public void notify(RemoteEvent anEvent) {
88
89 try {
90 System.out.println("Got event: " + anEvent.getSource() + ", " +
91 anEvent.getID() + ", " +
92 anEvent.getSequenceNumber() + ", " +
93 anEvent.getRegistrationObject().get());
94 } catch (Exception anE) {
95 System.out.println("Got event but couldn't display it");
96 anE.printStackTrace(System.out);
97 }
98 }
99 }
100
101 private static class Renewer extends Thread {
102 private SpaceImpl theSpace;
103 private SpaceUID theUID;
104
105 Renewer(SpaceImpl aSpace, SpaceUID aUID) {
106 theSpace = aSpace;
107 theUID = aUID;
108 }
109
110 public void run() {
111 try {
112 while (true) {
113 Thread.sleep(20000);
114
115 System.err.println("Renew");
116 theSpace.getLeaseControl().renew(theUID, 30000);
117 }
118
119 } catch (Exception anE) {
120 System.err.println("Failed to renew");
121 anE.printStackTrace(System.err);
122 System.exit(0);
123 }
124 }
125 }
126 }