0

I have recently wrote a very simple Restful service deployed to JBoss AS 7.

I have a JAX-RS interface defiled as (using scala):

@Provider
@Path("/customers")
trait ICustomerService {
  @GET
  @Path("/{id}")
  @Produces(Array("application/xml"))
  def getCustomer(@PathParam("id") id: Int): StreamingOutput
}

And a class implements it (using scala):

class ServiceFacade extends ICustomerService {
  val ctx = new ClassPathXmlApplicationContext("orderservice.xml")
  val customerService = ctx.getBean("customerService").asInstanceOf[CustomerService]

  def getCustomer(id: Int): StreamingOutput = {
    customerService.getCustomer(id)
  }
}

Here the problem comes. Everytime I issue a request from a client browser, a new ServiceFacade is created by Jboss, thus the Spring xml file is parsed once.

Is there anyway I can create the ServiceFacade myself in a spring config and simply let JBoss uses it rather than create for every single clieng request?

Many thanks.

Kevin
  • 5,972
  • 17
  • 63
  • 87

1 Answers1

0

You are creating a new Spring context on every instance creation of your ServiceFacade, Try either injecting the context or creating a singleton. I don't believe JAX-RS or RestEasy gurantees only a single instance of a annotated class is created.

Also, I am just getting up to speed on Scala myself, but should you not place the annotations on the implementation and not the trait?

Peter Cetinski
  • 2,328
  • 14
  • 8
  • Thanks for your reply. The way I want it to work is to create only one ServiceFacade myself using spring and somehow let the resteasy to use it, rather than let resteasy create it for every single request. Any idea? – Kevin Feb 25 '12 at 16:05
  • Take a look at my answer to related question: http://stackoverflow.com/questions/9009660/inject-spring-beans-into-resteasy/9445952#9445952. If that does not work for you, I know that Jersey provides a @Singleton annotation which forces the container to maintain only a single instance. I don't know if RestEasy provides the same. You could try using Jersey instead of the default JBoss RestEasy framework. – Peter Cetinski Feb 25 '12 at 16:53