Thursday 10 March 2016

WildFly 10.0 – EJB3.2 Bean and Client(Desktop/Standalone) sample code

 

Interface:
package com.server.myejb;

public interface EJBInterface {
    public String addValues(int i, int j);
}

implementation class: 
package com.server.myejb;

import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless(mappedName = "raviejb")
@Remote(EJBInterface.class)
public class EJBBean implements EJBInterface {

   
public String addValues(int i,int j){
        System.
out.println("Request received : i :"+i+"  j: "+j);
       
return "===========Message from Wildfly 10.0 server ========== Values added :"+(i+j);
    }
}
 
pom.xml
 
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xmlns="http://maven.apache.org/POM/4.0.0"

         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ejb3.bean</groupId>
    <artifactId>EJBBeanServer</artifactId>
    <version>1.0-SNAPSHOT</version>
   <packaging>ear</packaging>

    <properties>
       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <log4j2.version>2.5</log4j2.version>
        <java.source.version>1.8</java.source.version>
        <java.target.version>1.8</java.target.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.ejb</groupId>
            <artifactId>javax.ejb-api</artifactId>
            <version>3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.2</version>
        </dependency>
    </dependencies>
</project>

On Wildfly 10.0 server console, you can see the below comments after EJB3.2 deployment:

[org.jboss.as.ejb3.deployment] (MSC service thread 1-6) WFLYEJB0473: JNDI bindings for session bean named 'EJBBean' in deployment unit 'deployment "EJBBeanServer-1.0-SNAPSHOT"' are as follows:

                java:global/EJBBeanServer-1.0-SNAPSHOT/EJBBean!com.server.myejb.EJBInterface
                java:app/EJBBeanServer-1.0-SNAPSHOT/EJBBean!com.server.myejb.EJBInterface
                java:module/EJBBean!com.server.myejb.EJBInterface
                java:jboss/exported/EJBBeanServer-1.0-SNAPSHOT/EJBBean!com.server.myejb.EJBInterface
                java:global/EJBBeanServer-1.0-SNAPSHOT/EJBBean
                java:app/EJBBeanServer-1.0-SNAPSHOT/EJBBean
                java:module/EJBBean


Use the highlighted part from server console in the client code to lookup EJB:

package com.client.myejb;

import com.server.myejb.EJBInterface;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;import java.util.Properties;

public class EJBClient {

    public static void main(String...strings) throws NamingException {

        Context jndiContext = getInitialContext( );
        EJBInterface ref = (EJBInterface)jndiContext.lookup("ejb:/EJBBeanServer-1.0-SNAPSHOT/EJBBean!com.server.myejb.EJBInterface");
        System.out.println(ref);
        System.out.println(ref.addValues(20, 20));
        jndiContext.close();

    }

    public static Context getInitialContext( ) throws javax.naming.NamingException {

        Properties jndiProperties=new Properties();
        jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
        jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        jndiProperties.put(Context.PROVIDER_URL, "http-remoting://127.0.0.1:8080");
        jndiProperties.put("jboss.naming.client.ejb.context", true);
        return new InitialContext(jndiProperties);
    }
}

jboss-ejb-client.properties
 
endpoint.name=client-endpointremote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=falseremote.connections=defaultremote.connection.default.host=127.0.0.1remote.connection.default.port=8080remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false

Make sure the jboss-ejb-client.properties file is placed in the class path as mentioned in the below screenshots.


OUTPUT:
INFO: EJBCLIENT000013: Successful version handshake completed for receiver context EJBReceiverContext{clientContext=org.jboss.ejb.client.EJBClientContext@2a17b7b6, receiver=Remoting connection EJB receiver [connection=org.jboss.ejb.client.remoting.ConnectionPool$PooledConnection@4f063c0a,channel=jboss.ejb,nodename=dt148ga2]} on channel Channel ID 8bdb9633 (outbound) of Remoting connection 646d64ab to /127.0.0.1:8080
===========Message from Wildfly 10.0 server ========== Values added :40

Make sure, in the client code and properties file have the exact same fields as the sample code above. Otherwise we see the below error:

INFO: JBoss EJB Client version 2.1.4.Final
Proxy for remote EJB StatelessEJBLocator for "/EJBBeanServer-1.0-SNAPSHOT/EJBBean", view is interface com.server.myejb.EJBInterface, affinity is None
Exception in thread "main" java.lang.IllegalStateException: EJBCLIENT000025: No EJB receiver available for handling [appName:, moduleName:EJBBeanServer-1.0-SNAPSHOT, distinctName:] combination for invocation context org.jboss.ejb.client.EJBClientInvocationContext@c038203
                at org.jboss.ejb.client.EJBClientContext.requireEJBReceiver(EJBClientContext.java:798)
                at org.jboss.ejb.client.ReceiverInterceptor.handleInvocation(ReceiverInterceptor.java:128)
                at org.jboss.ejb.client.EJBClientInvocationContext.sendRequest(EJBClientInvocationContext.java:186)
                at org.jboss.ejb.client.EJBInvocationHandler.sendRequestWithPossibleRetries(EJBInvocationHandler.java:255)
                at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:200)
                at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:183)
                at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:146)
                at com.sun.proxy.$Proxy2.addValues(Unknown Source)
                at com.client.myejb.EJBClient.main(EJBClient.java:19)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:497)

  




\wildfly-10.0.0.Final\bin\client\jboss-client.jar set in the class path at client side.


2 comments: