How to add offline event handling in Openfire plugin?

274 Views Asked by At

I am java newbie . I need to handle offline sessions in Openfire plugin. From plugin development doc I am able to understand the basics , but I am not getting how can I handle offline event of openfire. There is a class "SessionEventDispatcher" in package org.jivesoftware.openfire.event

There we have following predefined events:

session_created
session_destroyed
anonymous_session_created
anonymous_session_destroyed
resource_bound

These events then have listener , which is implemented in the Presence plugin. Please help me understand how to proceed if I need to add offline event as well.

I am saving online users in redis but some how user sessions in openfire is less than users in redis, which means I am not handling some offline event in my plugin due to which user session is created and get added in redis , but user session is not closed explicitly which is handled to get remove from redis, and I face this discrepancy.

1

There are 1 best solutions below

0
On

please correct me if I don`t understand your question clearly. Do you examine interface org.jivesoftware.openfire.user.PresenceEventListener? For example I implement that interface :

public class CustomPresenceEventListener implements PresenceEventListener {

private static final Logger LOG = LoggerFactory.getLogger(CustomPresenceEventListener.class);

@Override
public void availableSession(ClientSession session, Presence presence) {
    LOG.info("\n\n=======SESSION AVAILABLE=========\n");
    try {
        LOG.info("USER : {}", session.getUsername());
    } catch (UserNotFoundException e) {
        LOG.info(e.getMessage(), e);
    }
    LOG.info("\n================\n\n");
}

@Override
public void unavailableSession(ClientSession session, Presence presence) {
    LOG.info("\n\n=======SESSION UNAVAILABLE=========\n");
    try {
        LOG.info("USER : {}", session.getUsername());
    } catch (UserNotFoundException e) {
        LOG.info(e.getMessage(), e);
    }
    LOG.info("\n================\n\n");
}

@Override
public void presenceChanged(ClientSession session, Presence presence) {
}

@Override
public void subscribedToPresence(JID subscriberJID, JID authorizerJID) {

}

@Override
public void unsubscribedToPresence(JID unsubscriberJID, JID recipientJID) {

}

And add/remove this listener with :

PresenceEventDispatcher.addListener(presenceEventListener);
PresenceEventDispatcher.removeListener(presenceEventListener);

And when I connect to Openfire I will see in the file info.log something like that : =======SESSION AVAILABLE========= 2017.03.29 14:27:01 .CustomPresenceEventListener - USER : 25

2017.03.29 14:27:01 .CustomPresenceEventListener -

And when I leave Openfire logs will be : =======SESSION UNAVAILABLE========= 2017.03.29 14:27:34 .CustomPresenceEventListener - USER : 25

2017.03.29 14:27:34 .CustomPresenceEventListener -

So you could add any action you want when user enter/leave Openfire.