Calling Empty Constructor in Jboss 7.1.1
sreeedhar Nov 29, 2013 12:17 AMHi
I wanted replace my code from Jboss 4.0 to Jboss 7.1.1 , in Jboss 7.1.1 when we call an object from stateful session bean class every time
it is invoking an empty constructor where it is not happening in Jboss 4.0. Did any one know the reason and what's new in Jboss 7.1.1
Home Interface
=============
package Books;
import javax.ejb.EJBHome;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
public interface BookHome extends EJBHome{
public Book create() throws RemoteException,CreateException;
}
Remote Interface
=================
package Books;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
import java.util.Vector;
public interface Book extends EJBObject{
public String addBook() throws RemoteException;
public General getGeneral() throws RemoteException;
}
Session Bean (Stateful)
====================
package Books;
import javax.ejb.SessionBean;
import java.util.Vector;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
public class BookBean implements SessionBean,java.io.Serializable{
Vector shelf;
int a,b;
private General g;
public BookBean()
{
}
public void ejbCreate()
{
System.out.println("Calling EJB Create method ========");
g =new General(this);
}
public String addBook()
{
System.out.println(" Addition of two Numbers ");
return String.valueOf(a+b);
}
public General getGeneral()
{
return g;
}
public void ejbRemove()
{
System.out.println("Calling ejbRemove method ==========");
}
public void ejbActivate()
{
System.out.println("Calling ejbActivate method ===========");
}
public void ejbPassivate()
{
System.out.println("calling ejbPassivate method ==========");
}
public void setSessionContext(SessionContext sc)
{
System.out.println("calling sessioncontext object =======");
}
}
General Class (calling from the Session Bean)
=================================
package Books;
public class General implements java.io.Serializable{
BookBean bb;
int a=10;
public General()
{
System.out.println("CallingEmptyConstructor in General");
a=20;
}
public General(BookBean bb)
{
this.bb=bb;
System.out.println("The BookBean Curren Object"+bb.toString());
System.out.println("Integer Variable Value is"+a);
}
public void show()
{
System.out.println("The content in the General Class");
System.out.println("Integer Variable Value is "+a);
}
}
jboss.xml
===================
<?xml version="1.0" encoding="UTF-8"?>
<jboss>
<enterprise-beans>
<session>
<ejb-name>book</ejb-name>
<jndi-name>Books.BookHome</jndi-name>
</session>
ejb-jar.xml
==================
<display-name>BookCatalog</display-name>
<enterprise-beans>
<session>
<ejb-name>book</ejb-name>
<home>Books.BookHome</home>
<remote>Books.Book</remote>
<ejb-class>Books.BookBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
</enterprise-beans>
</jboss>
Client (jsp file)
===================================
<html>
<head>
</head>
<body>
<%@page import="Books.Book" %>
<%@page import="Books.BookHome" %>
<%@page import="javax.naming.InitialContext" %>
<%
try {
InitialContext ctx = new InitialContext();
BookHome home = (BookHome) ctx.lookup("java:app/BookCatalog.jar/book!Books.BookHome");
int a=Integer.parseInt(request.getParameter("n1"));
int b=Integer.parseInt(request.getParameter("n2"));
Book book = home.create();
System.out.println("Book class ===============================>"+book);
book.getGeneral().show();
System.out.println("book.getGeneral()===="+book.getGeneral());
}
catch (Exception e) {
out.print(e.getMessage());
}
%>
</body>
</html>
If I execute this code in Jboss 4.0 empty constructor is not calling when it loads the object of General class
Jboss 4.0
===========
calling sessioncontext object =======
Calling EJB Create method ========
The BookBean Curren ObjectBooks.BookBean@1960caa
Integer Variable Value is10
Book class ===============================>Books.BookHome:hokz806s-4
The content in the General Class
Integer Variable Value is 10
book.getGeneral()====Books.General@170209f
but in the case of Jboss 7.1.1 it is calling empty constructor
Jboss 7.1.1
============
calling sessioncontext object =======
Calling EJB Create method ========
The BookBean Curren ObjectBooks.BookBean@5a138f
Integer Variable Value is10
Book class ===============================>Proxy for remote EJB StatefulEJBLocator{appName='sumear', moduleName='BookCatalog.jar', distinctName='', beanName='book', view='interface Books.Book', sessionId='{[-88, -110, 60, 15, 113, 127, 71, -127, -96, 6, 10, -106, -61, -23, 5, 33]}'}
CallingEmptyConstructor in General
The content in the General Class
Integer Variable Value is 10
CallingEmptyConstructor in General
book.getGeneral()====Books.General@16fbd74