Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should the Java main method be for a standalone application (for Spring JMS)?

Tags:

I am interested in creating a Spring standalone application that will run and wait to receive messages from an ActiveMQ queue using Spring JMS. I have searched a lot of places and cannot find a consistent way of implementing the main method for such a standalone application. There appears to be few examples of Spring standalone applications. I have looked at Tomcat, JBoss, ActiveMQ and other examples from the around the web but I have not come to a conclusion so ...

What is the best practice for implementing a main method for a Java application (specifically Spring with JMS) ?

Update: Here's an example from: http://forum.springsource.org/showthread.php?t=48197 Is this the best way of doing this?

public static void main(String args[]) {         try {            ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");             . . . . .             Object lock = new Object();             synchronized (lock) {                 lock.wait();               }          } catch (Exception e) {             e.printStackTrace();         } } 
like image 661
Brandon Avatar asked Mar 23 '10 04:03

Brandon


People also ask

Which is used to load a standalone application in spring?

Spring Boot makes it easy to create stand-alone, production-grade Spring-based applications. Spring Boot provides various starters for building standalone or more traditional war deployments.

What is Java main method?

static – Java's main method is static, which means no instances need to be created beforehand to invoke it. void – Some programming languages return a zero or 1 to indicate the main method has run successfully to complete. Java's main function is void, which means it does not return any value when it completes.

What is the function of main method?

When the Java interpreter executes an application (by being invoked upon the application's controlling class), it starts by calling the class's main method. The main method then calls all the other methods required to run your application.


2 Answers

When using Spring JMS you already use components/beans in your configuration that get auto-started and will stay alive (subscribe and read from queue/topic) until you stop the application.

To start and keep the application running, loading the applicationcontext should therefore be enough. Good practice though is to also call the registerShutdownHook, so on a application halt (i.e via ctrl+c in the console), al your beans are gracefully shutdown and disposed :)

public static void main(String args[]) {     AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");     context.registerShutdownHook(); } 
like image 129
momania Avatar answered Jun 01 '23 13:06

momania


This is what we have, inside app-context.xml we use spring JMS classes like (org.springframework.jms.listener.DefaultMessageListenerContainer to manage number of consumers and provide custom listener using org.springframework.jms.listener.adapter.MessageListenerAdapter)

app-context.xml contains all spring beans listeners and other stuff, the code below is bootstrapping Spring provided listeners on queues. So idea is to use Spring classes to manage multiple consumers. Let me know if this is what you need and need more information on configuring MessageListenerAdapter.

public static void main(String[] args) {      try     {         new ClassPathXmlApplicationContext("app-context.xml");      }     catch (Throwable e)     {         e.printStackTrace();         System.exit(-1);     }  } 
like image 27
soody Avatar answered Jun 01 '23 11:06

soody