BlazeDS and Java class in the WAR file


Hi I have a java class which has been deployed as WAR web application in a BlazeDS/Spring server sitting on JBOSS.

Apart from the Flex application which will access the WAR file, I also need to start some server side process's which will initiate BlazeDS "pushes" to the Flex client via AMF messaging.

What is the best way to implement this server side process? - Should it just be a class with a main() method in the WAR file which gets called from the command line? Can this be done - not sure you can run a class in a WAR file from command line? - Should it just be a class with a main() method in a JAR file which gets called from the command line?

Not sure what the standard practise here is. The key is that the process needs to be started on the BlazeDS server to push data out (not on the Flex client).

Any help would he appreacited Mike

First off, are you using the latest Spring/BlazeDS integration? If not, I highly recommend checking it out here. It can greatly simplify setting up message destinations for push messaging. It also will allow you to use JMS and Spring Integration message destinations as well as integrate Spring Security if you so choose.

Now to answer your question. What are the life-cycle requirements for your data push service? Do you want to be able to control the parameters of this data push (i.e., starting and stopping it, frequency, etc.) from other classes? Creating this service using Spring will allow you to inject it into other beans for control as you so desire.

I currently have a similar use case in which I use a BlazeDS message destination to "push" telemetry data to a client browser. I setup a "service" class that is instantiated by Spring as a singleton instance.

If you do not need external control of this singleton, I then suggest you use an annotated @PostConstruct or "init" method for creating a Thread and starting it with an anonymous Runnable representing your main loop. If your service needs to push data at a predefined frequency, you might consider a java.util.concurrent.ScheduledExecutorService.

Either way, you will also need to setup an annotated @PreDestory or "destroy" method that will execute just before the singleton instance is destroyed. This will allow you to insert code to safely stop the loop Thread or ScheduledFuture and clean up any necessary resources before the Spring container is shut down.

If you want further interaction with your service, you can manipulate it from other classes (such as Web controllers, etc.) using a service interface. Have your class implement this interface and inject your class into other classes using this interface. For a more daring solution, you might consider using dm Server or another OSGi container and create an OSGi service.

Please let me know if you need further help regarding this process or if there are specific details that I can illuminate further.