I have a JSP/Java web application which uses Jersey on a Tomcat server and this app must be live for months/years executing threads and maintaining live some objects.
What would be more efficient and safe? to use static methods on the jersey calls or to use instanced objects initialized on ServletContextListener contextInitialized() ?
Why?
Related
A web container will typically create a thread to handle each request. To ensure that a servlet instance handles only one request at a time, a servlet can implement the SingleThreadModel interface. If a servlet implements this interface, no two threads will execute concurrently in the servlet’s service method.A web container can implement this guarantee by synchronizing access to a single instance of the servlet or by maintaining a pool of web component instances and dispatching each new request to a free instance.
Could someone clarify the bolded part of the above paragraph ?
Doesn't each servlet would have only one instance of it in a web container?
Doesn't each servlet would have only one instance of it in a web container?
No, that is not always true. The web container manages the lifecycle of servlets (it is responsible for creating and managing instances of servlets). The Java EE specification does not guarantee that there will be only one instance of your servlet class, so you should not write your servlets in a way that depend on this.
As the description explains, the web container might make multiple instances of your servlet class to efficiently and concurrently handle requests if your servlet implements SingleThreadModel.
It is better to write your servlet in such a way that it does not need to implement SingleThreadModel - write your servlet code in a thread-safe way.
I am going through the book for "Java Web Services: Up and Running, 2nd Edition" and came across a statement that says:
A web server such as Tomcat can instantiate arbitrarily many instances
of a servlet, although the number is typically small (e.g., 1 through
4). The web server itself makes the decision.
So it means if I create a servlet then the server can create more than 1 instance but this is in contradiction to the explanation given in many posts for example if I check in this post : "10 clients requests for a Servlet.How many servlet instances are created" then it clearly states that:
Only one instance of servlet exist (per classloader) , and each
request will be served on its own thread
So please help me in understanding this, does a server can create more than 1 instance for a servlet?
The Servlet Specification states
For a servlet not hosted in a distributed environment (the default),
the servlet container must use only one instance per servlet
declaration. However, for a servlet implementing the
SingleThreadModel interface, the servlet container may instantiate
multiple instances to handle a heavy request load and serialize
requests to a particular instance.
In the case where a servlet was deployed as part of an application
marked in the deployment descriptor as distributable, a container may
have only one instance per servlet declaration per Java Virtual
Machine (JVM). However, if the servlet in a distributable
application implements the SingleThreadModel interface, the container
may instantiate multiple instances of that servlet in each JVM of the
container.
So it depends how you are deployed.
As suggested in the comments, SingleThreadModel has been deprecated for a long time.
by default servlets are intrinsic multithreaded. so only single instance be created and will be accessed by multiple threads.
I have one webserver with 2 instances of tomcat running. On each tomcat instance I have multiple web apps or web services.
What is the best way to call a function (or trigger some event with parameters) from a webapp of the first tomcat server on a webapp running on the second tomcat server. If it's for example a call using a url with parameters then this call should be secure and not accessible from outside the server.
I've read something about getting the servlet context but is this possible on different tomcat instances? Im thinking that this is only possible with webapps running in the same instance.
I dont want to use CORBA, RMI or SOAP because this is a bit oversized for my problem ... that is what Im thinking :)
Code examples are welcome. Thank you!
The ServletContext is only valid within the same container and can't be shared between two JVMs. The simplest method to do what you're asking is to just use some variety of RPC between the two containers, and RMI doesn't seem like particular overkill. The other usual approach would be a simple HTTP Web service (note the lowercase "s") that invokes your logic in the receiving container.
Spring's HTTPInvoker is great for this. You can use a Java interface, and your code on each instance doesn't need to know the call is remote - it just calls Java methods.
For security, you can use the Sun HTTP server on a different port (instead of using a servlet within Tomcat) and listen only on localhost.
Have a look here
http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/remoting.html#remoting-httpinvoker
Use Simple REST services , not that much secured .
Can i create and start a thread from inside a web service operation of a Java Web Application normally?If yes should that thread class be in the classes of the Web Application or the Java Application that consumes the service?
The server JVM will certainly need to load the classes used by your new thread, so they will need to be somewhere on the server classpath. Unless you also need to use some or all of them on the client application there will be no need for them to be on the client classpath. How you name and package them is up to you but if they are only used inside the server app it would make sense to use similar names or the same parent packages.
You should consider using something like Quartz to manage your threads and depending on what you want this web service to do, consider using JMS/MDB instead.
I'm new to both J2EE and WebLogic. I'd trying to determine the best way to implement a non-distributed cache (one cache per application instance) in a Java Web Services application running on WebLogic 10.3. I need to cache several different POJO's.
There will be multiple WebLogic instances running on each server in a cluster. When reading about ServletContext and InitialContext, I was a bit confused. I believe ServletContext is instance specific, but I can only access it from a Servlet, correct? I will need to access to the cache in separate threads so I'm not sure if this is possible outside of a Servlet.
I was reading a bit about JNDI, but it seems to work at the server or cluster level and not for each WebLogic/application instance.
Can anyone provide me with a suggestion and a code example to initialize, access, and destroy a cache of Java POJO's?
Thanks!
Leon
Here is an example on how to implement a method cache with Spring and EHCache:
http://opensource.atlassian.com/confluence/spring/display/DISC/Caching+the+result+of+methods+using+Spring+and+EHCache
The cache will be local if configured as in the example.
I am using this method in a web service client library to cache the results of a frequently used service that has nearly no update to its data.