Announcement Announcement Module
Collapse
No announcement yet.
Best practice to build non web server using Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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
      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.

      Comment


      • #4
        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?

        Comment


        • #5
          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 ... .

          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?

          Comment

          Working...
          X