简体   繁体   中英

How to create a listener to a method in java

I'm having a really bad time to implement a listener to a method.

In my current work i have to request a method every 5 seconds in order to verify for new information so i want to implement a method to notify me every time a new information come discarding the requesting method in every 5 seconds.

(I already read about observer pattern but had no successful in implementing.)

best regards

More information: For now im overriding this method from the smack XMPP library in order to store new messages in msgs Queue

public void processMessage(Chat chat, Message message) {
    if (message.getType() == Message.Type.chat) {
        req = message.getBody().toString();
        String[] temp = { chat.getParticipant().toString(), req };
        System.out.println(temp[0]+"says:"+temp[1]);
        synchronized (lock) {
            msgs.add(temp);
        }
    }
} 

and than i have this method executed every 0.5 seconds in order to verify new messages:

public String[] getMessage() {
    synchronized (lock) {
        String[] data;
        data=msgs.poll();
        return data;
    }
}

i'm trying to set a notification system that notifies me everytime the processMessage is executed.

Ok i manage to solve my problem.

I implemented a listener based on observer model. For that i had to implement a interface that i called XMPPmessageListener:

public interface XMPPmessageListener {
    public void readMsg(String senderJID, String msg); 
}

than in the XmppHandler class, the class of the methods processMessage and getMessage i added methods to add and remove listeners and a LinkedList to store the listeners:

private LinkedList<XMPPmessageListener> listeners = new LinkedList<XMPPmessageListener>();

public void addMsgListener(XMPPmessageListener listener){
    listeners.add(listener);
}

public boolean removeMsgListener(XMPPmessageListener listener){
    return listeners.remove(listener);
}       

than i did some exchanges in processMessage method in order to warn the listeners:

public void processMessage(Chat chat, Message message) {
    if (message.getType() == Message.Type.chat) {
        for(XMPPmessageListener l: listeners){
            l.readMsg(chat.getParticipant().toString(), message.getBody().toString());
        }
    }
} 

Now i can be notified at any method everytime a message is receive by creating a XMPPmessageListener and decide what to do with the message by overriding the readMsg method:

XmppHandler xmpp = new XmppHandler(XMPPuser, XMPPpassword, XMPPaddress, XMPPdomain, XMPPport);

XMPPmessageListener msglistener = new XMPPmessageListener() {

                @Override
                public void readMsg(String senderJID, String msg) {
                    String asw=xmlHandler.processMsg(msg,senderJID);

                }
            };
            xmpp.addMsgListener(msglistener);

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