6

I'm new with jax-rs and have build a web service with jersey and glassfish.

What I need is a method, which is called once the service is started. In this method I want to load a custom config file, set some properties, write a log, and so on ...

I tried to use the constructor of the servlet but the constructor is called every time a GET or POST method is called.

what options I have to realize that?

Please tell, if some dependencies are needed, give me an idea how to add it to the pom.xml (or else)

Marco Rehmer
  • 1,033
  • 2
  • 12
  • 32

1 Answers1

11

There are multiple ways to achieve it, depending on what you have available in your application:

Using ServletContextListener from the Servlet API

Once JAX-RS is built on the top of the Servlet API, the following piece of code will do the trick:

@WebListener
public class StartupListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        // Perform action during application's startup
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        // Perform action during application's shutdown
    }
}

Using @ApplicationScoped and @Observes from CDI

When using JAX-RS with CDI, you can have the following:

@ApplicationScoped
public class StartupListener {

    public void init(@Observes 
                     @Initialized(ApplicationScoped.class) ServletContext context) {
        // Perform action during application's startup
    }

    public void destroy(@Observes 
                        @Destroyed(ApplicationScoped.class) ServletContext context) {
        // Perform action during application's shutdown
    }
}

In this approach, you must use @ApplicationScoped from the javax.enterprise.context package and not @ApplicationScoped from the javax.faces.bean package.

Using @Startup and @Singleton from EJB

When using JAX-RS with EJB, you can try:

@Startup
@Singleton
public class StartupListener {

    @PostConstruct
    public void init() {
        // Perform action during application's startup
    }

    @PreDestroy
    public void destroy() {
        // Perform action during application's shutdown
    }
}

If you are interested in reading a properties file, check this question. If you are using CDI and you are open to add Apache DeltaSpike dependencies to your project, considering having a look at this answer.

Community
  • 1
  • 1
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • okay, I tried to use EJB 3.1 in my project for the last method, add Startup and Singleton but when I start the service the init() method isn't called. What did I missed? – Marco Rehmer Oct 02 '16 at 23:09
  • @Surras Try the first approach, using `ServletContextListener` from the Servlet API. – cassiomolin Oct 03 '16 at 07:59
  • I've tried the first one but I got an exception. I thougth it occours because I already use the jersey-servlet-container in my pom.xml and I can't load both, am I wrong? – Marco Rehmer Oct 03 '16 at 08:38
  • okay, now the first one worked (I've used the wrong dependency in pom.xml). But how can I declare variables which are accessable in my GET and POST methods (and which are singleton)? – Marco Rehmer Oct 03 '16 at 10:12
  • @Surras *how can I declare variables which are accessable in my GET and POST methods* Is really unclear to me. – cassiomolin Oct 03 '16 at 10:16
  • Okay sorry. What I mean is: at init I put an image path in a string variable for example and have a counter as integer, init with 0. When a POST method is called, I need to read the image path variable and increase the counter by 1. How can I declare and access these variables in my whole application? – Marco Rehmer Oct 03 '16 at 10:30
  • @Surras Use a singleton to hold this values. Or, depending on your requirements, use a database. However, I think it's a different question, outside of the scope of the original one. – cassiomolin Oct 03 '16 at 10:33
  • Thank you very much. I will ask a new question and leave the link as a comment here. – Marco Rehmer Oct 03 '16 at 11:50
  • I ask a new question for singleton variables. You can find the question here: http://stackoverflow.com/q/39835248/3603602 thanks for help! – Marco Rehmer Oct 03 '16 at 15:33