Communication between session beans

Hello,

I am working in a distributed aplication system with EJB, JBuilder X.

I have some design problems. I have a client who initializes a system. After this initialization several sessions beans are created. I do not have any problems to communicate the client with one of the session beans. But after the initialization, it is necessary that one session bean send a token to other session bean and i do not how implement this communication.

When the initializator client creates the beans i stored in every bean the address of the others beans in order to allow the further communication between beans, but when the communication between beans must start these address seems not be valid.

Any idea about how solve the problem?

Do you think that is possible to make this communication with session beans?

Thank you very much

[870 byte] By [javi_morenoa] at [2007-10-2 21:17:33]
# 1

> Do you think that is possible to make this

> communication with session beans?

Yes, you can pass an EJB handle to the session bean, which should invoke first bean.

In pseudo code:

// client code

EJBObj1 obj1 = createSessionBean1();

EJBObj2 obj2 = createSessionBean2();

Handle1 h1 = obj1.getHandle();

obj2.setHandleOfFirstBean(h1); // custom method in remote interface

// session bean 2

Handle h1;

h1.getEjbObject().anyRemoteMethodOfFirstBean();

http://java.sun.com/j2ee/1.4/docs/api/javax/ejb/Handle.html

Maris_Orbidansa at 2007-7-14 0:26:08 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Hello and thank you for your answer!

I tried your pseudocode and i think that either it is not totally correct or i made a mistake, becuase i obtain the next error:

ava.lang.ClassCastException

at subreconfiguration.TokenExchange.SendToken(TokenExchange.java:50)

at subreconfiguration.SubreconfigurationBean.SendToken(SubreconfigurationBean.java:68)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:324)

at com.inprise.ejb.ConcreteMethod.invoke(ConcreteMethod.java:32)

at com.inprise.ejb.EJBContext.invoke(EJBContext.java:166)

at com.inprise.ejb.Dispatcher.doInvoke(Dispatcher.java:1301)

at com.inprise.ejb.Dispatcher.invokeSecurityCheck(Dispatcher.java:1023)

at com.inprise.ejb.Dispatcher.invoke(Dispatcher.java:816)

at com.inprise.ejb.Dispatcher.invoke(Dispatcher.java:595)

at com.inprise.ejb.EJBHome.dispatcherInvokeBeanMethod(EJBHome.java:55)

at com.inprise.ejb.EJBHome$ComponentInterfaceMethodCache.invokeDispatcherMethod(EJBHome.java:1502)

at com.inprise.ejb.EJBHome.invokeDispatcherMethod(EJBHome.java:34)

at com.inprise.ejb.Dispatcher.invoke(Dispatcher.java:416)

at subreconfiguration.SubreconfigurationPOAInvokeHandler.SendToken(SubreconfigurationPOAInvokeHandler.java:37)

at subreconfiguration.SubreconfigurationPOAInvokeHandler.SendToken(SubreconfigurationPOAInvokeHandler.java:102)

at subreconfiguration.SubreconfigurationPOA._invoke(SubreconfigurationPOA.java:62)

at subreconfiguration.SubreconfigurationPOA._invoke(SubreconfigurationPOA.java:45)

at com.inprise.vbroker.poa.POAImpl.invoke(POAImpl.java:2693)

at com.inprise.vbroker.poa.ActivationRecord.invoke(ActivationRecord.java:109)

at com.inprise.vbroker.poa.ServerInterceptorManager$ARWrapper.invoke(ServerInterceptorManager.java:110)

at com.inprise.vbroker.GIOP.GiopProtocolAdapter.doRequest(GiopProtocolAdapter.java:824)

at com.inprise.vbroker.IIOP.ServerProtocolAdapter.doRequest(ServerProtocolAdapter.java:68)

at com.inprise.vbroker.GIOP.GiopProtocolAdapter.dispatchMessage(GiopProtocolAdapter.java:1106)

at com.inprise.vbroker.orb.TPDispatcherImpl$TPDispatcher.run(TPDispatcherImpl.java:106)

at com.inprise.vbroker.orb.ThreadPool$PoolWorker.run(ThreadPool.java:76)

Before read your answer I tried another way to solve my problem:

// client code

EJBObj obj1 = createSessionBean();

EJBObj obj2 = createSessionBean();

// session bean 2

obj1.anyRemoteMethodOfFirstBean();

and i obtained exactly the same error.

In both cases, i need to make a casting before called the anyRemoteMethodOfFirstBean, maybe, is here the error?

This casting would be something like:

//in my solution

((Bean Type)(obj1)).anyRemoteMethodOfFirstBean();

//or in your solution

((Bean Type)(h1.getEjbObject())).anyRemoteMethodOfFirstBean();

I used this kind of casting in the client-bean communication and it is working correctly.

Maybe is this casting the mistake? But i need make the casting to access to the anyRemoteMethodOfFirstBean

Thank you very much

javi_morenoa at 2007-7-14 0:26:08 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
I think that you should use a MDB for such stuff...btw do you want to invoke the secong Session Bean in a synchronous or asynchronous way? Another way might be use something like this@EJBprivate SessionRemote1 rem;
Hailstorma at 2007-7-14 0:26:08 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
> Maybe is this casting the mistake? No, it's correct, but without seeing all you'r code it's hard to tell where is the problem.
Maris_Orbidansa at 2007-7-14 0:26:08 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

Hello and thanks for you answer.

For your interest I think that you really think taht is possoble to commnunicates two session beans.

I am going to try to explain you waht i want to do:

I have just one session bean code:

I have a topology of nodes. Every node is called Subreconfigurator. i have a client called Initialiazator which starts the system and the nodes (the subreconfigurator) are going to be the session beans.An initial toplogy is load by the clientfrom a file. This initial topology is stored in a data structured in the client side. After that the client ompy this topology to every node. I dont have problems with this client - sesion bean comunication.

In this implementation phase i have to send/recieve a token throug all the topology. The token is initialized by tht initializator in one of the node and this node has to send the token to the next node and so on.Here is where i have the problem!! I obtained the previous post error when one node try to send the tokento the next one.

I write you the most importan code lines:

public class SubreconfigurationBean

implements SessionBean {

//ATRIBUTTES

ReconfiguratorTopology

private TokenExchange tokenExchange;

ReconfiguratorTopology reconfiguratorTopology;

SessionContext sessionContext;

private String nodeId;

//SOME METHODS...

public void getToken(Token token) {

System.out.println("getToken():The node that recive the token is" + this.nodeId);

tokenExchange.passingToken(token);

}

//Stupid method to send the token from the client side

public void SendToken() {

System.out.println("Stupid method to send the token from the client side" + this.nodeId);

tokenExchange.SendToken(nodeId, reconfiguratorTopology.getNodeList2());

public class TokenExchange extends SubreconfigurationBean{

//ATRIBUTES

private Token token; // Link to the token class

private boolean possessToken; // True if the SR associates a this TokenExachange class has tht token.

//METHODS

public TokenExchange() {

possessToken = false;

}

public void isEnableNextSR(){

}

public void SendToken(String nodeId, NodeList nodeList)

{

try {

System.out.println("ANTES");

//getNodeList();

System.out.println("DESPUES");

// Send the token to all the next SR(s) which are in current node (the current node is the

// node which has currently the token) connections list.

for (int j = 0; j < ((ConnectionsList)(nodeList.GetConnectionsList(nodeId))).size(); j++) {

//Check if SR j (check the connections)is enable or not.If not, dont send the token

System.out.println("Send token from "+ nodeId+ "to: " + (((ConnectionsList)(nodeList.GetConnectionsList(nodeId))).getNodeId(j)));

//missing code!!!!

//Send the token to node j.

String destine = ((ConnectionsList)(nodeList.GetConnectionsList(nodeId))).getNodeId(j);

//NodeList nl = getNodeList();

Object ad = (nodeList.GetAddress(destine));

//HERE IS WHERE MY PROGRAM CRASHES!!!!!!

((Subreconfiguration)(ad)).getToken(token);

}

// The actual token is referenced to NULL because token was pointing to

// the SR which HAD it. Now the token has been sent to the next SR(s).

token = null;

this.possessToken = false;

}

catch(Exception e) {

e.printStackTrace();

}

}

public void passingToken(Token receivedtoken)

{

System.out.println("passingToken");

token = new Token();

this.possessToken = true;

token = token.Clone(receivedtoken);

}

}

I hope that know my error is more understandable.

Thank you very much

javi_morenoa at 2007-7-14 0:26:08 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6
The best method is to call the Second session bean and pass it directly..Its safer in a distributed System@EJBprivate SessionSecond sec;sec.remoteMethod(YOUR PARAMETER);
Hailstorma at 2007-7-14 0:26:08 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...