Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Class data memeber sharing problem.  RSS feed

 
Vikas Kumar Sahu
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am facing one strange problem.

I am calling two functions of same session bean one by one from the client.
In one function I am doing some processing and populating one hashtable,
this hashtable is the data memeber of the class.
But when I am getting this hashtable refrence from the other method the populated
data is not comming. Its size is zero.

Can anybody have the solution ?

My client calling following method first -

readConfiguredWorkFlows(int userId)

//this method populating hashtable.

then taking refrence of the hashtable from the other method

getAllEccsCommandsHashtable()

but hashtable size is zero though its size is >0.

You can see the code below -


Code is like this -

public class WorkflowXmlManipulatorBean
implements SessionBean {
SessionContext sessionContext;
ArrayList workflowsJaxbArrayList = null;
Hashtable eccCommandsHashTable = new Hashtable();
Hashtable workflowPathHashTable = new Hashtable();

final String TOPDIRPATH = "config" + File.separator + "cm" + File.separator +
"ecc";
final String COMMANDDIRNAME = "commandxml";
final String WORKFLOWXMLDIRNAME = "workflowxml";

public void ejbCreate() throws CreateException {
}

public void ejbRemove() {
}

public void ejbActivate() {
}

public void ejbPassivate() {
}

/**
*
* @param sessionContext SessionContext
*/
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}

/**
* This function will read all xml file and put dom into arraylist
* @param userId int
* @return ArrayList
*/
public ArrayList readConfiguredWorkFlows(int userId) {
this.workflowsJaxbArrayList = new ArrayList();
visitAllDirsAndXMLFiles(new File(this.TOPDIRPATH));
return this.workflowsJaxbArrayList;
}


/**
* This function will visit all folders and subfolders and read workflows
* and make dom bjects and store it into the xml.
* @param dir File
*/

public void visitAllDirsAndXMLFiles(File dir) {
String dirName = dir.getName();

if (dir.isDirectory()) {
print("dir name =", dirName, '#');
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
visitAllDirsAndXMLFiles(new File(dir, children[i]));
}
}
else {
if (dirName.endsWith(".xml")) {
print("xml file name =", dirName, '&');
print("xml file path name =", dir.getPath(), '%');
print("Parent name", dir.getParentFile().getName(), '^');
if (dir.getParentFile().getName().equals(this.WORKFLOWXMLDIRNAME)) {
Unmarshaller ui = new Unmarshaller(Workflow.class);
ui.setValidation(false);
try {
Workflow workflow = (Workflow) ui.unmarshal(Workflow.class,
new FileReader(dir));
this.workflowsJaxbArrayList.add(workflow);
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (ValidationException ex) {
ex.printStackTrace();
}
catch (MarshalException ex) {
ex.printStackTrace();
}
catch (Exception ex) {
ex.printStackTrace();
}


}
else if (dir.getParentFile().getName().equals(this.COMMANDDIRNAME)) {
Unmarshaller ui = new Unmarshaller(Ne_root.class);
ui.setValidation(false);
try {
Ne_root ne_root = (Ne_root) ui.unmarshal(Ne_root.class,
new FileReader(dir));
ArrayList commandAl = new ArrayList();
for (int i = 0; i < ne_root.getOperationCount(); i++) {
Operation op = ne_root.getOperation(i);
System.out.println("op.getOperation_output()=" + op.getOperation_output());
commandAl.add(op.getOperation_output());
}
String eccname = dir.getParentFile().getParentFile().getName();
print("Eccname = ", eccname, '@');
this.eccCommandsHashTable.put(eccname, commandAl);
System.out.println("###::: hastable size =" + eccCommandsHashTable.size());

}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (ValidationException ex) {
ex.printStackTrace();
}
catch (MarshalException ex) {
ex.printStackTrace();
}
catch (Exception ex) {
ex.printStackTrace();
}


}
}
}

}


public Hashtable getAllEccsCommandsHashtable() {
return this.eccCommandsHashTable;
}

}


Thanx in advance-

Vikas Kumar Sahu
 
Rahul Devgan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vikas,
Well, this is not a problem but the way Session Beans work. Each method call from client is an individual call and the session bean considers them to be 2 different clients, for simplicity.
When the first method id called, a session bean object is pulled from the pool, services the request and is then sent back to pool, you can think of it on similar lines to being GC. When you call the SB next time, any SB object can be called from the pool and this will have everything given to it fresh, including your hashtable.
If, you need to populate data into a hastable in one call and use it another then you can cache the hastable on the client side and not on the bean side. There are several ways to achieve the same including using the Server's cache, if it provides one or simply creating a hastable in your client code and then passing it to second method.
Hope this clears things and is useful to you.
 
Vikas Kumar Sahu
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I have taken remote interface of this SB once,
and I think at this point of time the bean instance has created once.
Then I am caling these two method (with the help of this interface).
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Obtaining a component interface has nothing to do with the creation of bean instances. These instances are usually created on container startup. After the constructor runs, the setSessionContext() and ejbCreate() methods are invoked, after which the instances are in the method-ready state in the pool.

The easiest solution for you is probably for the first method to return the Hashtable to the client. Should the client wish to use this Hashtable in another EJB method, then the Hashtable can be passed in as an arg to this method.

Incidentally, your bean is in violation of an EJB programming restriction in that it uses the java.io package.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!