简体   繁体   中英

Exception creating ejb object when running client Main.java

This main is calling for a session-bean in the same project to send a message to a message-driven-bean in a separate project.

The other project is fully deployed and running without error.

For this project, the session-bean ejb can be deployed. The problem is when i try to run my client main code, netbeans can deploy the code but gives me (WARNING: Exception creating ejb object : [SSSbean]) upon running it. I simply don't see why the ejb object can't be created. Any ideas?

Session Bean is below:

public class SSSbean implements SSSbeanRemote {

@Resource(name = "jms/Topic")
private static Topic topic;
@Resource(name = "jms/TopicConnectionFactory")
private static ConnectionFactory topicFactory;

public SSSbean () {}

@Override
public void createMessage(String messageData) throws JMSException {
    Connection topicConnection = null;
    Session session = null;
    MessageProducer producer = null;

    topicConnection = topicFactory.createConnection();
    session = topicConnection.createSession(true,0);
    topicConnection.start();
    producer = session.createProducer(topic);

    TextMessage tm = session.createTextMessage();
    tm.setText(messageData);
    producer.send(tm);
}

@Override
@Remove
public void remove() {
    System.out.println("SSSBean:remove()");
}

}

Main is below:

public class Main {
    @EJB
    private static SSSbeanRemote ss;

    public static void main(String[] args) {
        // TODO code application logic here
        Main client = new Main();
        client.bX();
        ss.remove();
    }

    private void bX() {
        System.out.println("Main: Client started... ");

        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.println("Enter Suggestion: ");
            String suggestion = sc.nextLine();
            try{
                ss.createMessage(suggestion);
                continue;
            } catch (JMSException j) {
                System.out.println("Error: "+ j.toString());
            }
        } 
    }
}

You are trying to inject an EJB within a code that is not managed by the Container.

When your execute your client code, the main() method just ignores the @EJB annotation. The only one that knows what @EJB means and how to to inject a Bean is the Container. Try to execute the client code inside a Aplication Client Container

If you want to retrieve EJB bean from an application which is not managed by container, you can retrieve it from the InitialContext.

Maybe that will bring you little bit closer: https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM