Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning string from jms

I am writing a stand alone main method which invokes a producer (to push data into a queue), and then invokes a consumer which keeps listening to the topic.

I have overridden the onMessage and I am able to get the message from the queue, but I am not able to return the message to the calling method.

Actually, I want to carry the message to the browser, thus wanted to test if I can carry it atleast till main.

Please help;

class TextMessageListener implements MessageListener {
        String msgData;

        public String getMsgData() {
            return msgData;
        }

        public void setMsgData(String msgData) {
            this.msgData = msgData;
        }

        public void onMessage(Message message) {
            try {
                if (message instanceof TextMessage) {
                    TextMessage textMessage = (TextMessage) message;
                    System.out.println("Received message in  ::" + textMessage.getText() + " '");
                    setMsgData(textMessage.getText());
                }
            } catch (JMSException e) {
                System.out.println("Caught:" + e);
                e.printStackTrace();
            }
        }
    }
like image 652
Saurabh Jhunjhunwala Avatar asked May 05 '15 06:05

Saurabh Jhunjhunwala


1 Answers

Finally got the answer,

retrieving a value from message listener and print in Main

In this class, the user have given an example:

@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

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.

like image 193
Saurabh Jhunjhunwala Avatar answered Nov 04 '22 16:11

Saurabh Jhunjhunwala