1 22 package org.jboss.ejb3.test.clusteredsession; 23 24 import javax.ejb.Remote ; 25 import javax.ejb.Stateful ; 26 import javax.ejb.PostActivate ; 27 import javax.ejb.PrePassivate ; 28 import javax.ejb.Remove ; 29 import javax.ejb.EJBException ; 30 import javax.naming.InitialContext ; 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 ; 39 import org.jboss.logging.Logger; 40 41 47 @Stateful (name="testStateful") 48 @Clustered 49 @Interceptors({ExplicitFailoverInterceptor.class}) 51 @CacheConfig(maxSize=1000, idleTimeoutSeconds=3) @Remote (StatefulRemote.class) 53 public class StatefulBean implements java.io.Serializable , StatefulRemote 54 { 55 private Logger log = Logger.getLogger(StatefulBean.class); 56 private int counter = 0; 57 private String state; 58 public transient VMID myId = null; 59 public String name; 60 61 public int increment() 62 { 63 System.out.println("INCREMENT - counter: " + (counter++)); 64 return counter; 65 } 66 67 public String 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 79 public void longRunning() throws Exception 80 { 81 log.debug("+++ longRunning() enter "); 82 Thread.sleep(20000); 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 state) 97 { 98 this.state = state; 99 } 100 101 public String 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 120 public void postActivate() 121 { 122 ++postActivateCalled; 123 if (this.myId == null) 124 { 125 this.myId = new VMID (); 127 } 128 log.debug("Activate. My ID: " + this.myId + " name: " + this.name); 129 } 130 131 @PrePassivate 132 public void prePassivate() 133 { 134 ++prePassivateCalled; 135 log.debug("Passivate. My ID: " + this.myId + " name: " + this.name); 136 } 137 138 @Remove 139 public void remove() 140 { 141 } 142 143 @PostConstruct 144 public void ejbCreate() 145 { 146 this.myId = new VMID (); 147 log.debug("My ID: " + this.myId); 148 } 149 150 152 public NodeAnswer getNodeState() 153 { 154 if (this.myId == null) 155 { 156 this.myId = new VMID (); 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 name) 166 { 167 this.name = name; 168 log.debug("Name set to " + name); 169 } 170 171 public void setNameOnlyOnNode(String name, VMID node) 172 { 173 if (node.equals(this.myId)) 174 this.setName(name); 175 else 176 throw new EJBException ("Trying to assign value on node " + this.myId + " but this node expected: " + node); 177 } 178 179 public void setUpFailover(String failover) { 180 log.debug("Setting up failover property: " +failover); 182 System.setProperty ("JBossCluster-DoFail", failover); 183 } 184 185 } 186 | Popular Tags |