retrieving a value from message listener and print in Main

拜拜、爱过 提交于 2019-12-24 12:34:17

问题


I have 2 EJB app, A and B. A has a stateless session that send a message to app B (Message driven bean). App B send a message back to app A.

Now, I have the value I want in the message listener in the stateless session bean in A. But I need to show it from Main. I tried declaring a variable and store the value in it. But when I call it from Main, the value is lost.

@Stateful
public class AManagerBean implements ejb.AManagerRemote {
@Resource(mappedName = "jms/QueueConnectionFactory")
private ConnectionFactory queueConnectionFactory;
@Resource(mappedName = "jms/Queue")
private Queue queue;

private static int fineAmt;

......

static class AListener implements MessageListener{
    public void onMessage(Message message){
         .....
         fineAmt = msg.getInt("fineAmt"); 
        // I NEED FINEAMT TO SHOW IN MAIN CLASS

         .....
    }
}

public int returnFine(){
     return fineAmt;
 }

In the main class...

public class Main {

    @EJB
    public static AManagerRemote amr;

    public static void main(String[] args) {
         ......
         System.out.println(amr.returnFine());
         // ALWAYS RETURN 0

I need fineamt to show in main class. But it always return null.

How should I do it?


回答1:


I'm writing this as an answer, since it's pretty long for a comment, although it might not provide an actual answer.

First of all, non-final static variables in an EJB is disallowed. There's an entry about this in the EJB Restrictionc FAQ

Nonfinal static class fields are disallowed in EJBs because such fields make an enterprise bean difficult or impossible to distribute. Static class fields are shared among all instances of a particular class, but only within a single Java Virtual Machine (JVM ). Updating a static class field implies an intent to share the field's value among all instances of the class. But if a class is running in several JVMs simultaneously, only those instances running in the same JVM as the updating instance will have access to the new value. In other words, a nonfinal static class field will behave differently if running in a single JVM, than it will running in multiple JVMs. The EJB container reserves the option of distributing enterprise beans across multiple JVMs (running on the same server, or on any of a cluster of servers). Nonfinal static class fields are disallowed because enterprise bean instances will behave differently depending on whether or not they are distributed.

Second, you have defined a Stateful session bean. A stateful session bean is supposed to have conversational state, and a client (usually) have a handle to the same stateful bean for the duration of it's life time. I can't see anything conversational in your example (I assume, since you have cut out some code), so does it really need to be a stateful bean?

So I would suggest that the first thing you do is to do a re-design and try to get a more real life example up and running.



来源:https://stackoverflow.com/questions/15727989/retrieving-a-value-from-message-listener-and-print-in-main

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!