0

I am new to this field and i don't have any clue whatsoever on how to achieve the following goal.

I am taking an example to explain this. I need a java code which calls a rest api on some event. The event could be say a daily event like hit the api daily at noon or whenever the administrator registers a new event. I can write this code but this code should run forever i.e. whenever there is an event it should trigger the api hence in my understanding it must be put on a server.

Now here is my doubt say how to go about doing this. I have read that i could make a servlet and the request and response object would not do anything and since it will be a web application hence it would always be running in tomcat container on a web-server and i could stick my whole code there. But i do not know if this is the right way i am tackling the problem.

EDIT I am trying to implement a server which calls the Google cloud messaging api hence it has to send push notification to all the registered users and the user details are in mysql server. The administrator of this server has the right to register new event like what all notification he needs to send suppose he wants to send just one time notification like happy new year to all the registered user or say he wants to send a daily notification at a particular time say good morning. Now these are the events which has to be registered and listened by the server and on the occurance of event the server just has to make a JSONObject and call the GCM service. And also please guide me how to deploy this on server.

Hope it is much clear now

PS: Sorry if the question seems absurd bear with a novice and help me learn.

iec2011007
  • 1,828
  • 3
  • 24
  • 38
  • No need to run it as a server - an ordinary Java application may run endlessly just as well. Apart of that, very much everything depends on what kind of events you expect to support. The focus here should be on making your app so that it waits for an interrupt most of the time. – Jiri Tousek Jan 27 '16 at 17:26
  • First , your rest apis must be available all the time for this to work. Second, your event must call the apis. Can you be more specific about the kind of event ? – bluelurker Jan 27 '16 at 17:27
  • It is not absurd, but it *is* unclear. Please be elaborate on the expected input (how do you want to notify your app) and the expected output (what your "forever running" application do with the given input). Try to describe the real problem. – Gergely Bacso Jan 27 '16 at 17:29

1 Answers1

2

Although this is a question for programmers exchange probably the architecture could be something like this:

You dig in the java Timers and schedule (daily) events to call the rest api.

For accepting and scheduling new events and probably deleting old events you need an interface the administrator can use. This can be correctly as said a servlet with a form showing all scheduled events and deleting some of them or a rest api endpoint (so the admin should use something like wget to hit it).

You can use a database to persist events if you like for server restarts or use an in memory array variable to hold them if not.

arisalexis
  • 2,079
  • 2
  • 21
  • 42
  • Well i get the part for using java timers but i can not run the code on my machine and let the scheduler run i need it to run on server how to go about doing that, would tomcat suffice in deploying my plain old java code to server or have to look into other servers like glassfish. Please help how to deploy a simple java code which has timers and stuff to server so that it keeps running endlessly – iec2011007 Jan 31 '16 at 18:01
  • I think you need to get your hands dirty and program it. Tomcat,Glassfish or any other application server doesn't make a difference. – arisalexis Jan 31 '16 at 22:39
  • I mean how should it be done so that it is called. Should the code be stuffed in a separate thread in the init method of the default servlet or should something else be done ? – iec2011007 Feb 01 '16 at 10:34
  • yes you need to spawn a background thread. see http://stackoverflow.com/questions/1403755/how-to-create-a-thread-that-runs-all-the-time-my-application-is-running or http://stackoverflow.com/questions/4907502/running-a-background-java-program-in-tomcat (just a random quick search) but also research how it is done for your particular application server. – arisalexis Feb 01 '16 at 14:11