Menu

Integration with Quartz

Help
2006-12-09
2013-04-29
  • Matthew Kwong

    Matthew Kwong - 2006-12-09

    Hi, I am kind of newbie on these listener stuffs,

    First, I have tried all the examples, and it really works, nice library.

    However, all those examples are listening various events e.g. message received, loginCompleted etc... I want to integrate this with Quartz, so that the bot can send me message every, say like, 30 mins actively. Is there any addxxxx method to add an adapter like this?

    Thanks.
    Matthew Kwong

     
    • Matthew Kwong

      Matthew Kwong - 2006-12-10

      Hmm, I just create another messenger (ExternalMessageMessenger) which extends the SimpleMessenger and put a thread to listen another local port.

      Quartz then create a message every 30mins, send the message to that port.
      When the ExternalMessageMessenger get the message, it just triggers the "fireExternalMessageReceived" method:

      public void fireExternalMessageReceived(String email, String message) {
          for (Iterator iter = externalMessageListeners.iterator(); iter.hasNext();) {
              ExternalMessageListener listener = (ExternalMessageListener) iter.next();
              listener.externalMessageReceived(this, email, message);
          }
      }

      Then it is the normal driver program like EchoMessenger.java:

      ExternalMessageMessenger messenger = new ExternalMessageMessenger(Email.parseStr("xxx"),"xxx", 1234);
      messenger.setLogIncoming(true);
      messenger.setLogOutgoing(true);
      messenger.addExternalMessageListener(new ExternalMessageAdaptor() {
          public void externalMessageReceived(MsnMessenger messenger, String email, String message) {
              messenger.sendText(Email.parseStr(email), message);
          }
      });
      messenger.login();

      ============

      Is there any easier way to do this?

      My eclipse says "The constructor SimpleMessenger(Email, String) is not visible", so I added a "public" in front of that constructor, and rebuild. Is it right to do that?

      I was trying to use the SessionAcceptor from Cindy 3.0b2, and found out JML is using the old version of cindy (last Oct I think), any plan to upgrade that?

      I think this is not all related to Quartz, when System admins wants to parse those log file, and report the error message to their MSN, it can do this way too.

      Thanks.

      Matthew

       
      • Daniel Henninger

        For the 30 minute message, why not just use standard java Timer functionality?  Basically just create your messenger instance with a Timer that runs every 30 minutes and triggers message.sendText?

        As for SimpleMessenger, typically what one does is to create an MsnMessenger instance from the MsnMessengerFactory:
                msnMessenger = MsnMessengerFactory.createMsnMessenger(username, password);
                msnMessenger.setSupportedProtocol(new MsnProtocol[] { MsnProtocol.MSNP11 });
        And then work with that.

        Java-JML uses the 2 series of Cindy because it's labeled as stable.  =)  The 3 series is still labeled as early beta.  My plan is to wait until 3 matures.  =)

        There's actually a 'new' listener in Java-JML that you might be interested in that gives you access to cindy's session logging:
                msnMessenger = MsnMessengerFactory.createMsnMessenger(username, password);
                ((BasicMessenger)msnMessenger).addSessionListener(new MsnSessionListener(this));
                msnMessenger.setSupportedProtocol(new MsnProtocol[] { MsnProtocol.MSNP11 });

        Where MsnSessionListener is something like:
        public class MsnSessionListener extends SessionAdapter {

            public MsnSessionListener(MSNSession msnSession) {
                this.msnSession = msnSession;
            }

            /**
             * The session this listener is associated with.
             */
            public MSNSession msnSession = null;

            public void exceptionCaught(Session arg0, Throwable t) throws Exception{
                Log.debug("MSN: Session exceptionCaught for "+msnSession.getRegistration().getUsername()+" : "+t);
            }

            public void messageReceived(Session arg0, Message message) throws Exception {
                Log.debug("MSN: Session messageReceived for "+msnSession.getRegistration().getUsername()+" : "+message);
            }

            public void messageSent(Session arg0, Message message) throws Exception {
                Log.debug("MSN: Session messageSent for "+msnSession.getRegistration().getUsername()+" : "+message);
            }

            public void sessionIdle(Session session) throws Exception {
            }

            public void sessionEstablished(Session session) {
                Log.debug("MSN: Session established for "+msnSession.getRegistration().getUsername());
            }

            public void sessionTimeout(Session session) {
                // This is used to handle regular pings to the MSN server.  No need to mention it.
            }

            public void sessionClosed(Session session) {
                Log.debug("MSN: Session closed for "+msnSession.getRegistration().getUsername());
            }

        }

        Obviously you'll need to tweak that to your liking.  I'm using a special Log class from the project I'm using so that won't work for you.  =)  The most important ones you are probably looking for are messageSent and messageReceived.

         
    • Matthew Kwong

      Matthew Kwong - 2006-12-20

      >For the 30 minute message, why not just use standard java Timer functionality? Basically just create your messenger instance with a Timer that runs every 30 minutes and triggers message.sendText?

      Oops luckily I checked this forum again, and saw your reply on my question. If I use your approach, then the bot will go online for 2 secs and send the message, then go offline again?

      Actually I want the bot to stay online all the time, and every 30mins to send a message out. Anyway, thanks though. I have made that work with the messenger listening another local port to receive the event and do the sendText().

      Thank you again.
      Matthew Kwong

       
      • Daniel Henninger

        I don't see why it would have to log out and in.  =)  Basically log in, start a timer that routinely fires a sendText.  Basically you start the timer after you've established a session.  No need to log out and in.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.