retrieving a value from message listener and print in Main
Asked Answered
C

1

1

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?

Chymotrypsin answered 31/3, 2013 at 9:8 Comment(4)
It's quite difficult to understand what you're trying to do without seeing some code. Can you show us the code you have so far?Mercie
The use case is a bit weird, so I assume this is just for testing? Have you verified that onMessage is invoked?Mercie
@Mercie yes. I can print out the value at the server side already. I just need to show it in the Main class.Chymotrypsin
How do you run your Main class?Bonspiel
M
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.

Mercie answered 1/4, 2013 at 6:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.