1

This is a question about implementing a RESTful listener service, but I will provide some background first to provide context.

I have a existing java application & swing UI which takes a directory and database file as input. After these are set a button imports the PDF forms within that directory into the database. There is a small JFrame window in the UI for the buttons and some logging output. This works very well.

I now want this java app to connect to our Exchange Mail Server, listen for new emails with the PDFs attached, save them to a directory and import using my application above.

To connect to our exchange server (IMAP, POP etc are not enabled) I was going to use the ews-java API https://github.com/OfficeDev/ews-java-api

However this requires that the client who wants to get push notifications from the exchange mail server (via REST) does three things:

1) Connect to the exchange server (I can do this)

ExchangeService newService = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
newService.setUrl("https://myurl/ews/Exchange.asmx");
ExchangeCredentials credentials = new WebCredentials("user","password");
newService.setCredentials(credentials);

2) Subscribe to push notifications and setup a callback URI (I can do this)

    @Override
public PushSubscription subscribeToPushNotifications(){

URI callback = null;
PushSubscription pushSubscription = null;
try{
    logger.info("Subscribing to push notifications.. Endpoint Listener URI = " + config.getNotificationListenerURI());
    callback = new URI(config.getNotificationListenerURI());
    pushSubscription = service.subscribeToPushNotifications(
            getFoldersForSubscription(), callback , 5, null, 
            EventType.NewMail, EventType.Created, EventType.Deleted, EventType.Modified, EventType.Moved); 
    logger.info("Done! --> PushSubscription ID= " + pushSubscription.getId());
}catch(URISyntaxException e){
    logger.error("EWS ==> Invalid Notification Listener URL = " + config.getNotificationListenerURI() +". Please, verify <integration-modules>");
    Throwables.propagate(e);
}catch (Exception e){
    logger.error("EWS ==> Error subscribing to push notifications", e);
    Throwables.propagate(e);
}

return pushSubscription;
}

3) Setup a listener service to get new-email notifications (help needed here)

Similar to this : Exchange Web Services Java APi + RESTful Push Notifications Listener

@Path("/emailnotification")
public class ExchangeNotificationListener {

private static final Log logger = LogFactory.getLog(ExchangeNotificationListener.class);

@Path("/incomingevent")
@POST()
@Produces(MediaType.TEXT_XML)
public Response onNotificationReceived() throws Exception {
    logger.info("Received EWS notification !!!");
return Response.ok().build();
}

@GET
@Path("/{param}")
public Response getMsg(@PathParam("param") String msg) {
    String output = "Jersey say : " + msg;
    return Response.status(200).entity(output).build();
    }
}

It appears that my listener and jax-rs needs to run within a container like tomcat for step 3 and these web services that run within tomcat and are started by tomcat.

Is there a way to have my existing application start / stop the listener or do I have to convert the whole thing to be a web service project? So far I have only managed to do step 3 as a standalone web project but would like to be able to start stop it from my existing app.

Ideally I would like to have the ExchangeNotificationListener started after the subscription to email notifications in step two.

Could the class that starts the subscription extend the ExchangeNotificationListener class to do this?

Or should I be using something other than tomcat?

Thanks

Community
  • 1
  • 1

1 Answers1

0

I am not aware of any way to run a swing application and a servlet in the same Java process. Even if there was one it would probably be some kind of hack.

If you think about it, you will find that there are two completely separate concepts involved. Your Swing GUI is a desktop application, and desktop applications usually run in the context of the users session because they require a graphical desktop environment. So usually they will be started after the user has logged in and closed before the user logs out.

On the other hand your "listener" application is a web application, it needs a network connection and maybe file system access but otherwise it knows nothing about a graphical desktop environment. Web applications are normally longer running system services that are started when the server is booted and run as long as the server is running.

If you try to combine them into a single application then that also indicates a problem with your design.

I would instead recommend a different approach. Create the three parts for subscription, push-callback-registration and listener inside your web application. You can deploy that application in a Servlet container, install it as a regular system service and configure it to start it when the system boots up.

Next you need to think about what you do with mails you receive in the servlet. Either you store them somewhere and process them when the Swing app is started, or you also move your processing logic to the web application, preferably by moving that code into its own library that you can then access from both the gui application and the servlet.

Depending on which option you choose, when you start your Swing application either all your mails would already be in the database and you can simply access them, or you can just process those mails that the servlet has stored while your app was not running.

lanoxx
  • 12,249
  • 13
  • 87
  • 142