Results 1 to 5 of 5

Thread: Best practice to build non web server using Spring

  1. #1

    Default Best practice to build non web server using Spring

    We are trying to build a server (not web service related) using Spring framework. Yes, Spring provides many features in support of creating a web application. But is there any out of box features from Spring that can help to creat a non web related standalone server (or daemon)?

    For example, we simplely want a server that runs some scheduled job(using quartz), listens and handles Tibco messages..

    For this kind of server, what I can think of for now is something like this:

    Code:
    Public class MyServer{
    
    public static void main(String[] args) throws Exception {
            new ClassPathXmlApplicationContext(new String[]{"com/abc/serverContext.xml", "com/abc/quartzContext.xml});
            while(1){}
        }
    }
    Is this a good way to start? Any best practice?

  2. #2

    Default

    A quick update for those who are interested, the skeleton code can be as simple as this one:

    Code:
    Public class MyServer{
    
    public static void main(String[] args) throws Exception {
            new ClassPathXmlApplicationContext(new String[]{"com/abc/serverContext.xml", "com/abc/quartzContext.xml});
        }
    }
    To keep the server up and running without exitting, looks like there is no need for a loop (or calling wait on a object lock) after creating application context. I am not sure why that happens, but above code works like a server.

  3. #3
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Not this code, but rather one of the beans instantiated by this very context.
    From its name I may safely guess that this bean is quartz scheduler.

    To check try to run this code with empty context and program would exit immediately
    Quote Originally Posted by tannoy View Post
    A quick update for those who are interested, the skeleton code can be as simple as this one:

    Code:
    Public class MyServer{
    
    public static void main(String[] args) throws Exception {
            new ClassPathXmlApplicationContext(new String[]{"com/abc/serverContext.xml", "com/abc/quartzContext.xml});
        }
    }
    To keep the server up and running without exitting, looks like there is no need for a loop (or calling wait on a object lock) after creating application context. I am not sure why that happens, but above code works like a server.

  4. #4

    Default

    Quote Originally Posted by al0 View Post
    Not this code, but rather one of the beans instantiated by this very context.
    From its name I may safely guess that this bean is quartz scheduler.

    To check try to run this code with empty context and program would exit immediately
    You are right, it is actually becuase the Quartz bean SchedulerFactoryBean makes the program runs without exit.

    Another question is how to cleanly shutdown this kind of server. Ctrl-c will stop the program but I am not sure it is the right way to shutdown this kind of Spring program considering we use many resources in the context such as database connection, tibrv transport, cache etc. Any idea about best practice to shutdown a Spring context?

  5. #5
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    ConfigurableApplicationContext interface implemented by most if not all (I'm to lazy to check ) ApplicationContext implementations define close() method which in turn calls destroy() method of all singleton beans that implement DisposableBean interface along with methods specified as destroy-method inside bean definitions, e.g.

    Code:
         <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
            ...................
        </bean>
    Whe and how to call context close() method is up to you - you may listen fon some TCP-port, or periodically check for existence of some stop file, or spawn separate thread that waits for console input, and so forth ... .

    Quote Originally Posted by tannoy View Post
    You are right, it is actually becuase the Quartz bean SchedulerFactoryBean makes the program runs without exit.

    Another question is how to cleanly shutdown this kind of server. Ctrl-c will stop the program but I am not sure it is the right way to shutdown this kind of Spring program considering we use many resources in the context such as database connection, tibrv transport, cache etc. Any idea about best practice to shutdown a Spring context?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •