Win a copy of Modern frontends with htmx this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.rmi.RemoteExcepion. Working with StatefulSessionBean

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to implement a simple shopping cart using stateful Session Bean.
I am passing an item from the client and bean is supposed to add this item in an ArrayList object and return this ArrayList object.
I don't know what's going wrong. But when I run the client I get errors.

This is the code of the Session bean:
Code:

package cart.sessionbean;

import java.util.ArrayList;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
/**

/**
* @ejb.bean name="MyCartEJB"
*jndi-name="MyCartEJBBean"
*type="Stateful"
* @ejb.dao class="cart.sessionbean.MyCartEJBDAO"
* impl-class="cart.sessionbean.dao.MyCartEJBDAOImpl"
* @ejb.resource-ref res-ref-name="jdbc/OracleDS"
*res-type="javax.sql.Datasource"
*res-auth="Container"
* @jboss.resource-ref res-ref-name="jdbc/OracleDS" jndi-name="java:/OracleDS"
**/

public abstract class MyCartEJBBean implements SessionBean {

private ArrayList myCart;
protected SessionContext ctx;

/**
* The ejbCreate method
* @ejb.create-method
*
*/
public void ejbCreate (ArrayList myCart)throws
javax.ejb.CreateException
{
System.out.println("Entering MyCartEJBBean.ejbCreate");
this.myCart=myCart;
System.out.println("Leaving MyCartEJBBean.ejbCreate");
}


public void setSessionContext(javax.ejb.SessionContext ctx)
{
this.ctx=ctx;
}

public void unsetSessionContext()
{
this.ctx=null;
}

/**
* @ejb.interface-method
*view-type="remote"
* @dao.call name="addToCart"
**/
public java.util.ArrayList addToCart(String itemID,int quantity)
{
return null;
}

/**
* @ejb.interface-method
* view-type="remote"
*/
public void setMyCart(ArrayList myCart) {
this.myCart = myCart;
}

/**
* @ejb.interface-method
* view-type="remote"
*/
public ArrayList getMyCart() {
return myCart;
}

}




and this is the client:
Code:


package cart.client;

import java.rmi.RemoteException;
import java.util.Hashtable;
import java.util.ArrayList;

import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import cart.sessionbean.dao.CartContents;
/**
* @author Abhishek
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class ShoppingCartClient {

CartContents cartObject=new CartContents();
ArrayList resultCart=new ArrayList();
int size=0;

private cart.sessionbean.MyCartEJBHome getHome() throws NamingException {
return (cart.sessionbean.MyCartEJBHome) getContext().lookup(
cart.sessionbean.MyCartEJBHome.JNDI_NAME);
}
private InitialContext getContext() throws NamingException {
Hashtable props = new Hashtable();

props.put(
InitialContext.INITIAL_CONTEXT_FACTORY,
"org.jnp.interfaces.NamingContextFactory");
props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");

InitialContext initialContext = new InitialContext(props);
return initialContext;
}
public void testBean() {

try {
cart.sessionbean.MyCartEJB myBean = getHome().create(resultCart);

//--------------------------------------
//This is the place you make your calls.
System.out.println("Adding item to cart");
resultCart=myBean.addToCart("id001",1);
System.out.println("Going to Sleep for 1 min ......................... " );
Thread.sleep(60000); // sleep for 1 minute.
//System.out.println("Reply from bean after 1min " + myBean.getMyCart());
resultCart=myBean.getMyCart();
size=resultCart.size();
for(int i=0;i<size;i++)
{
cartObject=(CartContents)resultCart.get(i);
System.out.println(cartObject.getItemid());
System.out.println(cartObject.getQuantity());

}

System.out.println("Going to Sleep for 1 min ......................... " );
Thread.sleep(60000);
System.out.println("Adding item to cart");
resultCart=myBean.addToCart("id002",2);
resultCart=myBean.getMyCart();
for(int i=0;i<size;i++)
{
cartObject=(CartContents)resultCart.get(i);
System.out.println(cartObject.getItemid());
System.out.println(cartObject.getQuantity());

}

} catch (RemoteException e) {
e.printStackTrace();
} catch (CreateException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
} catch (InterruptedException e)
{

}
}

public static void main(String[] args) {
ShoppingCartClient test = new ShoppingCartClient();
test.testBean();

}
}




And this is the error message I am getting when I run the client:

Code:

Adding item to cart
java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.ServerException: RuntimeException; nested exception is:
java.lang.ClassCastException
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:292)
at sun.rmi.transport.Transport$1.run(Transport.java:148)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
at java.lang.Thread.run(Thread.java:534)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:247)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:223)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:133)
at org.jboss.invocation.jrmp.server.JRMPInvoker_Stub.invoke(Unknown Source)
at org.jboss.invocation.jrmp.interfaces.JRMPInvokerProxy.invoke(JRMPInvokerProxy.java:135)
at org.jboss.invocation.InvokerInterceptor.invoke(InvokerInterceptor.java:87)
at org.jboss.proxy.TransactionInterceptor.invoke(TransactionInterceptor.java:46)
at org.jboss.proxy.SecurityInterceptor.invoke(SecurityInterceptor.java:45)
at org.jboss.proxy.ejb.StatefulSessionInterceptor.invoke(StatefulSessionInterceptor.java:104)
at org.jboss.proxy.ClientContainer.invoke(ClientContainer.java:85)
at $Proxy1.addToCart(Unknown Source)
at cart.client.ShoppingCartClient.testBean(ShoppingCartClient.java:52)
at cart.client.ShoppingCartClient.main(ShoppingCartClient.java:93)
Caused by: java.rmi.ServerException: RuntimeException; nested exception is:
java.lang.ClassCastException
at org.jboss.ejb.plugins.LogInterceptor.handleException(LogInterceptor.java:360)
at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:195)
at org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invoke(ProxyFactoryFinderInterceptor.java:12
2)
at org.jboss.ejb.StatefulSessionContainer.internalInvoke(StatefulSessionContainer.java:410)
at org.jboss.ejb.Container.invoke(Container.java:674)
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 org.jboss.mx.capability.ReflectedMBeanDispatcher.invoke(ReflectedMBeanDispatcher.java:284)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:549)
at org.jboss.invocation.jrmp.server.JRMPInvoker.invoke(JRMPInvoker.java:359)
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 sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:261)
at sun.rmi.transport.Transport$1.run(Transport.java:148)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:144)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:460)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:701)
at java.lang.Thread.run(Thread.java:534)
Caused by: java.lang.ClassCastException
at cart.sessionbean.MyCartEJBSession.getDao(MyCartEJBSession.java:44)
at cart.sessionbean.MyCartEJBSession.addToCart(MyCartEJBSession.java:56)
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 org.jboss.ejb.StatefulSessionContainer$ContainerInterceptor.invoke(StatefulSessionContainer.java
:878)
at org.jboss.ejb.plugins.SecurityInterceptor.invoke(SecurityInterceptor.java:117)
at org.jboss.resource.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionIntercep
tor.java:186)
at org.jboss.ejb.plugins.StatefulSessionInstanceInterceptor.invoke(StatefulSessionInstanceIntercept
or.java:271)
at org.jboss.ejb.plugins.AbstractTxInterceptor.invokeNext(AbstractTxInterceptor.java:84)
at org.jboss.ejb.plugins.TxInterceptorCMT.runWithTransactions(TxInterceptorCMT.java:243)
at org.jboss.ejb.plugins.TxInterceptorCMT.invoke(TxInterceptorCMT.java:104)
at org.jboss.ejb.plugins.LogInterceptor.invoke(LogInterceptor.java:191)
... 21 more




Please help me. Thanks,
Abhishek (IJ)
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are getting ClassCastException.

private cart.sessionbean.MyCartEJBHome getHome() throws NamingException {
return (cart.sessionbean.MyCartEJBHome) getContext().lookup(
cart.sessionbean.MyCartEJBHome.JNDI_NAME);
}



use PortableRemoteObject.narrow() to typecast -

Object ref= getContext().lookup(
cart.sessionbean.MyCartEJBHome.JNDI_NAME);

MyCartEJBHome mycart =(cart.sessionbean.MyCartEJBHome)PortableRemoteObject.narrow(ref,MyCArtEJBHome.class);

return mycart;
 
Abhishek Asthana
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You wrote:


use PortableRemoteObject.narrow() to typecast -

Object ref= getContext().lookup(
cart.sessionbean.MyCartEJBHome.JNDI_NAME);

MyCartEJBHome mycart =(cart.sessionbean.MyCartEJBHome)PortableRemoteObject.narrow(ref,MyCArtEJBHome.class);

return mycart;



I am unable to understand where to keep this code.
I kept it in the client :



But it doesn't help. The same errors come. Please tell me more. You know I am complete newbee and a partailly complete duffer.
Thanks,
Abhishek.
 
Abhishek Asthana
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have tested a stateless session bean which returns ArrayList object containing elements of type ItemDetails (a class). I have not even narrowed the object, yet it works fine.

private myDAO.bean.DataEJBHome getHome() throws NamingException {
return (myDAO.bean.DataEJBHome) getContext().lookup(
myDAO.bean.DataEJBHome.JNDI_NAME);
}

It is working perfectly.
What can it be with stateful beans?
Abhishek
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic