help me in Stateful beans
Question 1: As per the book I follow to learn EJB author told that every lookup creates a new stateful session bean. So, what I did is defined a method init() with @PostConstruct annotation with a sysout statement. So, that need to be executed for every Stateful bean instantiated. But the same is not happening. Consider the following code
In Bean
@Stateful
public class PersonnelModelBean implements PersonnelModelRemote{
@PostConstruct
void init(){
System.out.println("STATEFUL BEAN transforming to Mathod Ready state");
}
}
On Client side
try {
InitialContext context = InitialContextBuilder.getJBOSSInitialContext();
PersonnelModelRemote personnelModel = (PersonnelModelRemote)context.lookup("PersonnelModel/remote");
personnelModel.setPersonKey(new Integer(1));
personnelModel.setPersonName("Naresh");
PersonData person = personnelModel.getPerson();
System.out.println(person);
personnelModel = (PersonnelModelRemote)context.lookup("PersonnelModel/remote");
personnelModel.setPersonKey(new Integer(2));
personnelModel.setPersonName("Pokuri");
person = personnelModel.getPerson();
System.out.println(person);
} catch (NamingException e) {
e.printStackTrace();
}
Question 2: In the same way author told that on calling @Remove annotated method would remove client associated bean from container. So, the container has to call destroy()(which is annotated with @PreDestroy) method on deleting bean. But that is not happening. Consider the following code
@Stateful
public class PersonnelModelBean implements PersonnelModelRemote{
@Remove
public PersonData getPerson() {
PersonData personData = new PersonData();
personData.setKey(key);
personData.setName(name);
return personData;
}
@PreDestroy
void destroy(){
System.out.println("STATEFUL BEAN 开发者_如何学Gotransforming to Does not exist state");
}
}
Question 3: I have set to 60 sec in standardjboss.xml of server/default/conf dir of JBOSS. I have waited for 15 min and on executing the client code it should call @PostConstruct annotated method as told in the book. But that was also not happening.
Q1 : this should work, the method with the PostConstruct annotation should be called before any other calls reach the SFSB. Does your client work? Maybe the log message goes to server.log
Q2: The method with a remove annotation is called before the container removes the bean from the container-pool. It is not intended to use with business methods. Check http://www.java2s.com/Code/Java/EJB3/RemoveAnnotation.htm
Q3: The clean-up may be delayed depending on your settings in $JBOSS_HOME/server/defalut/conf/standardjboss.xml Check the cache policy settings in this section:
<container-configuration>
<container-name>Standard Stateful SessionBean</container-name>
<call-logging>false</call-logging>
...
Maybe you can force the container to destroy unneeded beans (just for testing) by setting
<container-pool-conf>
<MaximumSize>1</MaximumSize>
</container-pool-conf>
and use a second client.
精彩评论