Cannot implement Java remote interface to communicate with EJB application running on Websphere Liberty

88 views Asked by At

I want to expose EJB's running on Websphere Liberty via a remote interface. I have created a test EJB app and a test client. My client seems to connect to Liberty, but the bean is not being created. Below is the error.

Mar 14, 2023 7:51:47 AM com.sun.corba.se.impl.transport.SocketOrChannelConnectionImpl <init>
WARNING: "IOP00410201: (COMM_FAILURE) Connection failure: socketType: IIOP_CLEAR_TEXT; hostname: localhost; port: 0"
org.omg.CORBA.COMM_FAILURE:   vmcid: SUN  minor code: 201  completed: No
    at com.sun.corba.se.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2200)
    at com.sun.corba.se.impl.logging.ORBUtilSystemException.connectFailure(ORBUtilSystemException.java:2221)
    at com.sun.corba.se.impl.transport.SocketOrChannelConnectionImpl.<init>(SocketOrChannelConnectionImpl.java:223)
    at com.sun.corba.se.impl.transport.SocketOrChannelConnectionImpl.<init>(SocketOrChannelConnectionImpl.java:236)
    at com.sun.corba.se.impl.transport.SocketOrChannelContactInfoImpl.createConnection(SocketOrChannelContactInfoImpl.java:119)
    at com.sun.corba.se.impl.protocol.CorbaClientRequestDispatcherImpl.beginRequest(CorbaClientRequestDispatcherImpl.java:187)
    at com.sun.corba.se.impl.protocol.CorbaClientDelegateImpl.request(CorbaClientDelegateImpl.java:137)
    at com.sun.corba.se.impl.protocol.CorbaClientDelegateImpl.is_a(CorbaClientDelegateImpl.java:229)
    at org.omg.CORBA.portable.ObjectImpl._is_a(ObjectImpl.java:130)
    at org.omg.CosNaming.NamingContextHelper.narrow(NamingContextHelper.java:69)
    at com.sun.jndi.cosnaming.CNCtx.callResolve(CNCtx.java:504)
    at com.sun.jndi.cosnaming.CNCtx.lookup(CNCtx.java:555)
    at com.sun.jndi.toolkit.url.GenericURLContext.lookup(GenericURLContext.java:205)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at work.Main.lookupRemoteBean(Main.java:49)
    at work.Main.testDefaultContextLookupWithCorbanameNameService(Main.java:39)
    at work.Main.main(Main.java:28)
Caused by: java.net.BindException: Cannot assign requested address: connect
    at sun.nio.ch.Net.connect0(Native Method)
    at sun.nio.ch.Net.connect(Net.java:454)
    at sun.nio.ch.Net.connect(Net.java:446)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:648)
    at java.nio.channels.SocketChannel.open(SocketChannel.java:189)
    at com.sun.corba.se.impl.transport.DefaultSocketFactoryImpl.createSocket(DefaultSocketFactoryImpl.java:95)
    at com.sun.corba.se.impl.transport.SocketOrChannelConnectionImpl.<init>(SocketOrChannelConnectionImpl.java:207)
    ... 14 more

I am following samples per: https://github.com/OpenLiberty/open-liberty/blob/release/dev/com.ibm.ws.ejbcontainer.remote_fat/test-applications/RemoteClientWeb.war/src/com/ibm/ws/ejbcontainer/remote/client/web/RemoteTxAttrServlet.java

The following is my EJB code.

    package test;

    import javax.ejb.Stateless;

    @Stateless(mappedName = "CalcImpl")
    public class CalcImpl implements CalcRemote, CalcLocal {
    
        @Override
        public int add(int a, int b) {
            return a + b;
        }   
    }

And the remote interface is as follows.

    package test;
    
    import javax.ejb.Remote;
    
    @Remote
    public interface CalcRemote {
        public int add(int a, int b);
    }

The stand-alone client app is as follows.

    package work;
    
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.rmi.PortableRemoteObject;
    
    import test.CalcRemote;
    
    public class Main {
    
        private static Context defaultContext;
        private static final Integer IIOPPort = new Integer("2809");
        private static final String CorbaName = "corbaname::localhost:" + IIOPPort;
        private static final String CorbaNameNS = CorbaName + "/NameService";
        
        private static final String App = "app1EAR";
        private static final String Module = "app1EJB";
        private static final String CalcBean = "CalcImpl";
    
        private static final String CalcBeanJndi = "ejb/global/" + App + "/" + Module + "/" + CalcBean;
    
        public static void main(String[] args) {
    
            Main m = new Main();
            
            try {
                m.testDefaultContextLookupWithCorbanameNameService();
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
        
        public void testDefaultContextLookupWithCorbanameNameService() throws Exception {
            
            String jndiName = CorbaNameNS + "#" + CalcBeanJndi + "!" + "test\\.CalcRemote";
            System.out.println("jndiName=" + jndiName);
            CalcRemote bean = lookupRemoteBean(getDefaultContext(), jndiName, CalcRemote.class);
            if (null == bean) {
                System.out.println("Remote bean is null");
                System.exit(1);
            }
            System.out.println("bean.add 1,2 = " + bean.add(1, 2));
        }
        
        public static <T> T lookupRemoteBean(Context context, String jndiName, Class<T> interfaceClass) throws NamingException {
            System.out.println("lookupRemoteBean: JNDI = " + jndiName + ", interface = " + interfaceClass.getName());
            Object remoteObj = context.lookup(jndiName);
            CalcRemote bean = (CalcRemote) remoteObj;
            System.out.println("bean=" + bean.getClass().getName());
            System.out.println("lookupRemoteBean: found = " + ((remoteObj == null) ? "null" : remoteObj.getClass().getName()));
            T remoteBean = interfaceClass.cast(PortableRemoteObject.narrow(remoteObj, interfaceClass));
            System.out.println("lookupRemoteBean: returning = " + ((remoteBean == null) ? remoteBean : remoteBean.getClass().getName()));
            return remoteBean;
        }
    
        private static Context getDefaultContext() throws NamingException {
    
            if (defaultContext == null) {
                defaultContext = new InitialContext();
            }
            return defaultContext;
        }
            
    }

The variable jndiName per the above resolves as follows.

corbaname::localhost:2809/NameService#ejb/global/app1EAR/app1EJB/CalcImpl!test.CalcRemote

It seems to be that I am close to getting things working, but not quite there yet. Is there something that I have not setup on Liberty? Why the error 'COMM_FAILURE' and 'port: 0'?

I'm really stuck. Have found similar problems elsewhere on stackoverflow, but no resolutions that help me. Help appreciated. Thanks

Update. Discovered that all works fine via the following examples. Just need to make sure all lines up correctly. Remote EJB examples

0

There are 0 answers