0

I come from a website programming background and I recently started to learn Java with the primary intention of being able to create a Back-end "Scheduling" service. This means that users should be able to create/configure methods to be run at specific date/times.

The solution must work on for a Windows OS.

I understand a number of required concepts;

  • Singleton Javabean
  • SOAP/REST Web Services

The plan is that my PHP front-end would need to communicate with a Java SOAP/REST Web Service which would, in turn, communicate with a "Singleton Javabean", The Javabean must start at boot time. I need another Bean to loop infinitely that would read SNMP(and other network messaging systems) and have those values read into a Web Service.

In the future I wish that these solutions to be able to be read into an Android Application.

However, I don't know how I would run a method that listened for specific times from a config file updated from a database or have a Bean run as a service to facilitate reading SNMP.

I've looked into some Java Servers, like JBoss, Glassfish and Tomcat but the documentation is so vast that it is intimidating to troll though to find out if they support the features I require.

I don't require a step-by-step guide here, just a push in the correct direction.

How would I configure a Javabean that listens for specific times? How would I configure a Javabean to run as a service to facilitate reading SNMP in an infinite loop?

tkw83
  • 185
  • 1
  • 5
glend
  • 1,592
  • 1
  • 17
  • 36
  • 1
    I suggest you look at Apache Camel with the SNMP component. This will allow you to create a SNMP server that listens to it. Secondly Apache Camel has the ability to host web services by using CXF. Thus you dont need to code a singleton bean ad use the JEE specification. – Namphibian Jan 20 '15 at 20:12

2 Answers2

0

I certainly agree with your statement that the documentation is vast. Unfortunately I think you're going to have to deal with that problem head on. As for scheduling a task, you could use Spring's Task Scheduling feature. This will work on any webapp, but it will also work without a webapp.

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
0

Executor

For the periodic execution of the task in Java, learn about the ScheduledExecutorService. Search StackOverflow for many examples and discussion.

besides that class, you will find much discussion about the Timer class. But Timer is older, less flexible, and most importantly, should not be used in a Servlet environment.

The tricky thing is learning how to properly handle thread-safety. By definition you have multiple threads working with the same data.

Servlet

Those servers you mentioned such as Tomcat and Jetty are based on Java Servlet technology. Servlets are an alternative to your PHP.

If you want to keep your PHP, you can use one of those Servlet servers only as a Web Service server. So you will not need to master all that documentation.

Launch

The way to kook into your web app launching in a Java Servlet container is to make a class that implements the ServletContextListener interface. A pair of methods will be called by the container, one when the web app is being deployed (launching) and another when the web app is being I deployed (shutdown).

Again, search StackOverflow for many examples and discussion.

No Need For Singleton

No need for a Singleton. Singletons "are evil" as some people say (such as Miško Hevery), making testing difficult and keeping the instance in memory endlessly. Having a single instance is not the problem, it is the Java implementation of the Singleton Design Pattern keeping an instance in a static var that is the problem.

ServletContext

You can access the ServletContext to store and retrieve instances for your entire web app. Call the setAttribute and getAttribute methods to pass any Object subclass you want.

Annotations

Be sure to learn about annotations in Java. They can greatly reduce the configuration work of your server and web service. For example, @WebService and @SOAPBinding(style=Style.RPC).

Java Enterprise Edition

Java version numbers are confusing, and there is more than one kind ("edition") of Java.

Regular "desktop" or "standard" Java follows certain number scheme. The "enterprise" Java ("Java Enterprise Edition" or "Java EE") built on top of standard Java follows its own numbering scheme. So currently as of 2015-01 we have Java Standard Edition 8 but Java Enterprise Edition 7 (tutorial) (built on Java SE 7).

Be sure to look at the information on Java EE 7 rather than 6. I'm not an expert on publishing web services with Java. But I believe you’ll find many advances that may be easier now in Java EE 7.

Furthermore, there is a lighter smaller version of Java EE 7 called "Web Profile" that supports Servlets and a handful of additional technologies. The full Java EE layers on dozens of other technologies as well. For a list of those Web Profile technologies, see my answer on another question.

Tomcat & Jetty are both outstanding products. But both Tomcat & Jetty aim at mainly Servlets, that being a subset of the Web Profile which turn is a subset of full Java EE. You may, or may not, find the additional features of the Web Profile or full Java EE useful for publishing web services. In that case you may want to use the full Java EE servers such as TomEE or Glassfish or others. Or, sometimes the lighter way to go is to start with Tomcat or Jetty, cherry-pick a few of those additional technologies, and add their jar files to your project.

In particular, you may find JAX-WS useful (tutorial, reference implementation). Or if you prefer REST style, look at Java API for RESTful Services, JAX-RS.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Just to make sure I understand you fully, in short use Tomcat as a Web Service and make a class that implements the ServletContextListener interface in the Tomcat server and use setters and getters to modify the ServletContextListener Class? – glend Jan 21 '15 at 01:38
  • @psycotik Close. The `ServletContext` object you retrieve so you can save your "global" data, calling get/setAttribute. "Context" means "the world your app knows about, the stuff your app can see". The `ServletContextListener` is the class *you* implement, with a pair of methods that will be called by the container when web app launches/shuts down. – Basil Bourque Jan 21 '15 at 04:13
  • this Question & Answer of mine, [How to access `ServletContext` from within a Vaadin 7 app?](http://stackoverflow.com/q/27933599/642706), interesting. – Basil Bourque Jan 21 '15 at 23:01
  • Great video, a real eye opener. – glend Jan 22 '15 at 14:01
  • The comment above must be referring to [this video presentation](http://youtu.be/-FRm3VPhseI) by Google staffer Miško Hevery explaining why the Singleton design pattern "is evil". As an alternative to Singleton usage in a Java web app, place any needed single instances on the `ServletContext` object by calling its `get/setAttribute` methods. – Basil Bourque Jan 22 '15 at 17:08