Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Starting a servlet from another class

 
Al Grant
Greenhorn
Posts: 9
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to use this library to connect Java to a Microsoft Exchange Webserver and get notified of a new email. ( https://github.com/OfficeDev/ews-java-api/wiki )

The process is basically (1) Connect to Exchange Server (2) Subscribe to notifications (3) Setup a listener to receive notifications. (1) and (2) I have done and are largely handled by the library - step 3 is a little harder. I have copied the code here :

http://stackoverflow.com/questions/33784552/exchange-web-services-java-api-restful-push-notifications-listener

Where he connects (1):




Then subscribes:



Obviously you need to make sure your callback address is reachable by the server - I am all ok up to this point, but then I need to create a servlet, which in Eclipse I am doing through 'Dynamic Web Project':



I am using Apache and Jersey, and I know this is working because I can browse to localhost:8080/com.vogella.jersey.first/rest/emailnotification/abc123 and my browser outputs "Jersey say: abc123"

But I have a few questions:

How do you start the listener service from an existing application? ie what is the line of code to start ExchangeNotificationListener?

Thanks in advance.

AG
 
Paul Clapham
Sheriff
Posts: 21572
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't start a servlet. The servlet is part of a web application installed in a web application server, in your case Jersey. The application server manages the servlet, including initializing it, and it's not the responsibility of any other code to manage it.

I suspect that isn't the answer to the question you wanted to ask, though.

I'm not quite familiar with the programming style you're using, but I think that if you want to cause that servlet's onNotificationReceived() method to be called then you would have to send a POST request to localhost:8080/com.vogella.jersey.first/rest/emailnotification/incomingevent -- was that what you wanted to ask, or am I still off course?
 
Paul Clapham
Sheriff
Posts: 21572
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And... if you wanted all of that setup code to be run when the servlet starts, then in the old style of servlets you would put the code into its init(ServletConfig) method. But I don't know if Jersey handles that sort of thing differently.
 
Al Grant
Greenhorn
Posts: 9
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:You don't start a servlet. The servlet is part of a web application installed in a web application server, in your case Jersey. The application server manages the servlet, including initializing it, and it's not the responsibility of any other code to manage it.

I suspect that isn't the answer to the question you wanted to ask, though.

I'm not quite familiar with the programming style you're using, but I think that if you want to cause that servlet's onNotificationReceived() method to be called then you would have to send a POST request to localhost:8080/com.vogella.jersey.first/rest/emailnotification/incomingevent -- was that what you wanted to ask, or am I still off course?


Hi Paul,

Thanks for the reply. Your right - thats not quite the answer I was looking for, though I am slowly understanding better this whole tomcat thing.

I guess ultimately the guy who wrote that stack exchange post presumably has a way of starting his listener, and stopping it?

I already have a little application in java, and want to be able to start listening for new emails (then doing business logic to do with saving attachments) and be able to stop listening.

How would I go about this?

Thanks in advance

Al
 
Paul Clapham
Sheriff
Posts: 21572
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Al Grant wrote:I guess ultimately the guy who wrote that stack exchange post presumably has a way of starting his listener, and stopping it?


I suppose so... did they explicitly say they were using a servlet to encapsulate the listener? I ask because if you're looking for push notifications then why would you use a servlet? A servlet is designed to send you data only when you request that data, which is not what push notification is at all.

I already have a little application in java, and want to be able to start listening for new emails (then doing business logic to do with saving attachments) and be able to stop listening.

How would I go about this?


An application... that's a much better idea. You put steps (1) and (2) into the application's initialization code, which it sounds like you did that already. Then, well I'm not too clear on how you get a listener from the subscription or how you attach a listener to it. My guess is that you have to write your own class which subclasses, or implements, ExchangeNotificationListener, or something like that, and add it to the subscription using a method named something like addListener. Your onNotificationReceived method is then going to be called whenever a notification is received and your method can then do whatever it wants. Although I would have expected that method to include a parameter containing the details of the notification which it's telling you was received.

Sorry to be so vague, but doesn't the place you got that code from expand it to a simple application to listen for and report on notifications?
 
Al Grant
Greenhorn
Posts: 9
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Al Grant wrote:I guess ultimately the guy who wrote that stack exchange post presumably has a way of starting his listener, and stopping it?


I suppose so... did they explicitly say they were using a servlet to encapsulate the listener? I ask because if you're looking for push notifications then why would you use a servlet? A servlet is designed to send you data only when you request that data, which is not what push notification is at all.

I already have a little application in java, and want to be able to start listening for new emails (then doing business logic to do with saving attachments) and be able to stop listening.

How would I go about this?


An application... that's a much better idea. You put steps (1) and (2) into the application's initialization code, which it sounds like you did that already. Then, well I'm not too clear on how you get a listener from the subscription or how you attach a listener to it. My guess is that you have to write your own class which subclasses, or implements, ExchangeNotificationListener, or something like that, and add it to the subscription using a method named something like addListener. Your onNotificationReceived method is then going to be called whenever a notification is received and your method can then do whatever it wants. Although I would have expected that method to include a parameter containing the details of the notification which it's telling you was received.

Sorry to be so vague, but doesn't the place you got that code from expand it to a simple application to listen for and report on notifications?


Hi Paul,

I got the code from here:

http://stackoverflow.com/questions/33784552/exchange-web-services-java-api-restful-push-notifications-listener

The background is my current application has a UI which allows a user to select a Directory containing PDF forms which have been filled out by a user.

It also has a button to select a database and a log window.

When the user hits the "RUN" button it imports the fields and data into the database.

The purpose of the listener is to get a push notification when a new email arrives and if some business logic is met then the attachment is saved, and the existing import code run over it to get it into the database.

So I can add step 1 and step 2 to the existing application, but how to get a listener from the subscription as you say is the issue?

Regards,

AG
 
Paul Clapham
Sheriff
Posts: 21572
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would go looking for the actual documentation for that product, tutorials for it, and so on. Trying to write an entire program based on a single question about the product isn't the best way to proceed.
 
Al Grant
Greenhorn
Posts: 9
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I would go looking for the actual documentation for that product, tutorials for it, and so on. Trying to write an entire program based on a single question about the product isn't the best way to proceed.


The docs are located here:

https://github.com/OfficeDev/ews-java-api/wiki/Getting-Started-Guide#using-push-notifications-with-the-ews-java-api

But guess what, here is a quote:

"The EWS Java API does not provide a built-in push notifications listener. It is the responsibility of the client application to implement such a listener."

THANKS MICROSOFT.

Hence the need to work out how to implement this listener.

AG
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic