0
votes

I use EJB3+JavaEE6+JBoss. I am absolutely newbie in EJB. I wrote this code:

package server.ejb;
@Remote
public interface HelloUser
{
    void sayHello( String name );
}

@Stateless
public class HelloUserBean implements HelloUser
{
    @Override public void sayHello( String name )
    {
        System.out.println( "Hello " + name );
    }
}

Having assebled this code with Maven and deployed it on JBoss, I wrote a client:

import server.ejb.HelloUserBean;
import javax.ejb.EJB;

public class Test
{
    @EJB
    public static HelloUserBean bean;

    public static void main( String... args )
    {
        bean.sayHello( "Alex" );
    }
}

After compiling, I've got NullPointerException. It said that bean was null. Using JDNI + PersistentContext I could get a success, but I still can't use DI as well. Please, help me


I reorginized my code! Actually I wrote another server-side project with the same sence and a standalone client-app. Here is the structure of server-side app: enter image description here

@Remote
public interface EchoRemote{
    String getMessage();
}

@Stateless
public class EchoBean implements EchoRemote{
    @Override
    public String getMessage(){
        return "Hello From Stateless Bean";
    }
}

public class InvokationClient{
    @EJB
    private EchoRemote bean;

    public String getMessage(){
        return bean.getMessage();
    }
}

And here is the client-side standalone app:

import com.steeplesoft.client.InvokationClient;

public class Main{
    public static void main( String... args ) throws IOException{
        InvokationClient client = new InvokationClient();

        FileWriter fileWriter = new FileWriter( "D:/invokation_client_test.txt" );
        fileWriter.write( client.getMessage() );
        fileWriter.close();
    }
}

I've got empty file and NullPointerEception in console

I hope you can help me :) It's tremendously important for me!!!

1

1 Answers

0
votes

So you start your Test-class standalone in a separate JVM. Where did you configure to which JBoss it should connect? Which component does the dependency injection? Since you don't have a DI container that manages the Test-class and since the connection to JBoss is not configured anywhere, this can't work.

In order to make it work, you can do the following:

1) Write a Servlet, use @EJB in the Servlet and deploy it on JBoss. Put your EJB and the Servlet in the same WAR to make it easy. The Servlet is managed by the container and DI works. As a newbie with EJB I would do this first.

2) Do a JNDI-Lookup and call your EJB from a standalone client as described in https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI

3) Use an Application Client Container (ACC) as described in http://blogs.steeplesoft.com/posts/2011/02/22/java-ees-buried-treasure-the-application-client-container/ Deploy the EAR to jboss and invoke the client locally

$JBOSS_HOME/bin/appclient.sh --host remote://localhost:4447 ./local/path/to/enterpriseapplication-0.1-SNAPSHOT.ear#appclient-0.1-SNAPSHOT.jar

Remark: When I tried the example from blogs.steeplesoft.com, I had problems with the Swing classes, but it did work JBoss EAP 6.2, when I removed the Swing classes.