Java – returns a string from JMS

I'm writing an independent main method that calls a producer (pushing data into a queue) and then calls a consumer who listens to the topic continuously.

I have overridden onmessage. I can get the message from the queue, but I can't return the message to the calling method

In fact, I want to pass this information to the browser, so I want to test whether I can send it to 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();
            }
        }
    }

Solution

Finally got the answer,

retrieving a value from message listener and print in Main

In this class, the user gives 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, non - final static variables in EJBs are not allowed There is an entry in the EJB restrictive FAQ

Informal static class fields are not allowed in EJBs because they make it difficult or impossible for enterprise beans to distribute Static class fields are shared among all instances of a particular class, but only in a single Java virtual machine (JVM) Updating static class fields means the intention of sharing field values among all instances of the class However, if a class runs in multiple JVMs at the same time, only those instances running in the same JVM as the update instance can access the new value In other words, if you run in a single JVM, the behavior of non - virtual static class fields will be different from running in multiple JVMs The EJB container retains the option to distribute enterprise beans on multiple JVMs (running on the same server or any server) Fields that are not final static classes are not allowed because the behavior of enterprise bean instances will vary depending on whether they are distributed Second, you have defined a stateful session bean A stateful session bean should have a conversation state, and the client (usually) has a handle to the same state bean for the duration of its life cycle I can't see any dialogue in your example (I assume, because you have deleted some code), so does it really need a stateful bean?

So the first thing I suggest you do is do a redesign and try to make a more real life example rise and run

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>