KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > clusteredsession > StatefulBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.test.clusteredsession;
23
24 import javax.ejb.Remote JavaDoc;
25 import javax.ejb.Stateful JavaDoc;
26 import javax.ejb.PostActivate JavaDoc;
27 import javax.ejb.PrePassivate JavaDoc;
28 import javax.ejb.Remove JavaDoc;
29 import javax.ejb.EJBException JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.annotation.PostConstruct;
32 import javax.interceptor.Interceptors;
33
34 import org.jboss.annotation.ejb.Clustered;
35 import org.jboss.annotation.ejb.cache.tree.CacheConfig;
36 import org.jboss.system.server.ServerConfig;
37
38 import java.rmi.dgc.VMID JavaDoc;
39 import org.jboss.logging.Logger;
40
41 /**
42  * SFSB interface
43  *
44  * @author Ben Wang
45  * @version $Revision: 46483 $
46  */

47 @Stateful JavaDoc(name="testStateful")
48 @Clustered
49 // Mimic explict failover
50
@Interceptors({ExplicitFailoverInterceptor.class})
51 @CacheConfig(maxSize=1000, idleTimeoutSeconds=3) // this will get evicted the second time eviction thread wakes up
52
@Remote JavaDoc(StatefulRemote.class)
53 public class StatefulBean implements java.io.Serializable JavaDoc, StatefulRemote
54 {
55    private Logger log = Logger.getLogger(StatefulBean.class);
56    private int counter = 0;
57    private String JavaDoc state;
58    public transient VMID JavaDoc myId = null;
59    public String JavaDoc name;
60
61    public int increment()
62    {
63       System.out.println("INCREMENT - counter: " + (counter++));
64       return counter;
65    }
66
67    public String JavaDoc getHostAddress()
68    {
69       return System.getProperty(ServerConfig.SERVER_BIND_ADDRESS);
70    }
71
72    public static int postActivateCalled = 0;
73    public static int prePassivateCalled = 0;
74
75    /**
76     * Sleep to test
77     * @throws Exception
78     */

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

152    public NodeAnswer getNodeState()
153    {
154       if (this.myId == null)
155       {
156          //it is a failover: we need to assign ourself an id because of transient nature
157
this.myId = new VMID JavaDoc();
158       }
159
160       NodeAnswer state = new NodeAnswer(this.myId, this.name);
161       log.debug("getNodeState, " + state);
162       return state;
163    }
164
165    public void setName(String JavaDoc name)
166    {
167       this.name = name;
168       log.debug("Name set to " + name);
169    }
170
171    public void setNameOnlyOnNode(String JavaDoc name, VMID JavaDoc node)
172    {
173       if (node.equals(this.myId))
174          this.setName(name);
175       else
176          throw new EJBException JavaDoc("Trying to assign value on node " + this.myId + " but this node expected: " + node);
177    }
178
179    public void setUpFailover(String JavaDoc failover) {
180       // To setup the failover property
181
log.debug("Setting up failover property: " +failover);
182       System.setProperty ("JBossCluster-DoFail", failover);
183    }
184
185 }
186
Popular Tags