KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > clusteredsession > nested > ParentStatefulBean


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.ejb3.test.clusteredsession.nested;
9
10 import org.jboss.annotation.ejb.Clustered;
11 import org.jboss.annotation.ejb.cache.tree.CacheConfig;
12 import org.jboss.logging.Logger;
13 import org.jboss.system.server.ServerConfig;
14 import org.jboss.ejb3.test.clusteredsession.ExplicitFailoverInterceptor;
15 import org.jboss.ejb3.test.clusteredsession.NodeAnswer;
16 import org.jboss.ejb3.test.clusteredsession.ClusteredStatelessRemote;
17
18 import javax.interceptor.Interceptors;
19 import javax.annotation.PostConstruct;
20 import javax.ejb.EJB JavaDoc;
21 import javax.ejb.Stateful JavaDoc;
22 import javax.ejb.Remote JavaDoc;
23 import javax.ejb.PostActivate JavaDoc;
24 import javax.ejb.PrePassivate JavaDoc;
25 import javax.ejb.Remove JavaDoc;
26 import javax.ejb.EJBException JavaDoc;
27 import java.rmi.dgc.VMID JavaDoc;
28
29 /**
30  * SFSB with nested bean
31  *
32  * @author Ben Wang
33  * @version $Revision: 46483 $
34  */

35 @Stateful JavaDoc(name="testParentStateful")
36 @Clustered
37 // Mimic explict failover
38
@Interceptors({ExplicitFailoverInterceptor.class})
39 @CacheConfig(maxSize=1000, idleTimeoutSeconds=3) // this will get evicted the second time eviction thread wakes up
40
@Remote(ParentStatefulRemote.class)
41 public class ParentStatefulBean implements java.io.Serializable JavaDoc, ParentStatefulRemote
42 {
43    private static Logger log = Logger.getLogger(ParentStatefulBean.class);
44    private int counter = 0;
45    private String JavaDoc state;
46    public transient VMID JavaDoc myId = null;
47    public String JavaDoc name;
48
49    @EJB JavaDoc
50    private NestedStateful nested;
51
52    @EJB JavaDoc
53    private ClusteredStatelessRemote slsb;
54
55    public int increment()
56    {
57       counter = nested.increment();
58
59       log.debug("INCREMENT - parent counter: " + counter);
60       return counter;
61    }
62
63    public String JavaDoc getHostAddress()
64    {
65       return System.getProperty(ServerConfig.SERVER_BIND_ADDRESS);
66    }
67
68    public static int postActivateCalled = 0;
69    public static int prePassivateCalled = 0;
70
71    /**
72     * Sleep to test
73     * @throws Exception
74     */

75    public void longRunning() throws Exception JavaDoc
76    {
77       log.debug("+++ longRunning() enter ");
78       Thread.sleep(10000);
79       log.debug("+++ longRunning() leave ");
80    }
81
82    public int getPostActivate()
83    {
84       return ParentStatefulBean.postActivateCalled;
85    }
86
87    public int getPrePassivate()
88    {
89       return ParentStatefulBean.prePassivateCalled;
90    }
91
92    public int getNestedPostActivate()
93    {
94       return NestedStatefulBean.postActivateCalled;
95    }
96
97    public int getNestedPrePassivate()
98    {
99       return NestedStatefulBean.prePassivateCalled;
100    }
101
102    public void setState(String JavaDoc state)
103    {
104       this.state = state;
105    }
106
107    public String JavaDoc getState()
108    {
109       log.debug("getState(): entering ...");
110       return this.state;
111    }
112
113    public NodeAnswer getStatelessState() {
114       return slsb.getNodeState();
115    }
116
117    public void reset()
118    {
119       state = null;
120       counter = 0;
121       ParentStatefulBean.postActivateCalled = 0;
122       ParentStatefulBean.prePassivateCalled = 0;
123    }
124
125    public void resetActivationCounter()
126    {
127       ParentStatefulBean.postActivateCalled = 0;
128       ParentStatefulBean.prePassivateCalled = 0;
129       NestedStatefulBean.postActivateCalled = 0;
130       NestedStatefulBean.prePassivateCalled = 0;
131    }
132
133    @PostActivate JavaDoc
134    public void postActivate()
135    {
136       ++ParentStatefulBean.postActivateCalled;
137       if (this.myId == null)
138       {
139          //it is a failover: we need to assign ourself an id
140
this.myId = new VMID JavaDoc();
141       }
142       log.debug("Activate. My ID: " + this.myId + " name: " + this.name);
143    }
144
145    @PrePassivate JavaDoc
146    public void prePassivate()
147    {
148       ++ParentStatefulBean.prePassivateCalled;
149       log.debug("Passivate. My ID: " + this.myId + " name: " + this.name);
150    }
151
152    @Remove JavaDoc
153    public void remove()
154    {
155    }
156
157    @PostConstruct
158    public void ejbCreate()
159    {
160       this.myId = new VMID JavaDoc();
161       log.debug("My ID: " + this.myId);
162    }
163
164    // Remote Interface implementation ----------------------------------------------
165

166    public NodeAnswer getNodeState()
167    {
168       if (this.myId == null)
169       {
170          //it is a failover: we need to assign ourself an id
171
this.myId = new VMID JavaDoc();
172       }
173
174       NodeAnswer state = new NodeAnswer(this.myId, this.name);
175       log.debug("getNodeState, " + state);
176       return state;
177    }
178
179    public void setName(String JavaDoc name)
180    {
181       this.name = name;
182       log.debug("Name set to " + name);
183    }
184
185    public void setNameOnlyOnNode(String JavaDoc name, VMID JavaDoc node)
186    {
187       if (node.equals(this.myId))
188          this.setName(name);
189       else
190          throw new EJBException JavaDoc("Trying to assign value on node " + this.myId + " but this node expected: " + node);
191    }
192
193    public void setUpFailover(String JavaDoc failover) {
194       // To setup the failover property
195
log.debug("Setting up failover property: " +failover);
196       System.setProperty ("JBossCluster-DoFail", failover);
197    }
198 }
199
Popular Tags