Created
September 13, 2014 11:24
-
-
Save abhirockzz/ecc5a94b288622e75376 to your computer and use it in GitHub Desktop.
Demonstration of Container managed concurrency in Singleton Beans (default, locking type not explicitly specified )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.abhirockzz.wordpress.ejb.lifecycle.singleton; | |
import com.abhirockzz.wordpress.ejb.lifecycle.stateful.MyStatefulBean; | |
import java.util.Date; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.ejb.Singleton; | |
import javax.ejb.Startup; | |
@Singleton | |
@Startup | |
public class MySingletonBean { | |
public void act() { | |
System.out.println("Entered MySingletonBean/act() on " + new Date().toString() + " . Singleton instance " + this.hashCode() + " Thread : " + Thread.currentThread().getName()); | |
try { | |
Thread.sleep(2000); | |
} catch (InterruptedException ex) { | |
Logger.getLogger(MyStatefulBean.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
System.out.println("Exit MySingletonBean/act() on " + new Date().toString() + " . Singleton instance " + this.hashCode() + " Thread : " + Thread.currentThread().getName()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.abhirockzz.wordpress.ejb.lifecycle.singleton; | |
import java.io.IOException; | |
import java.util.Date; | |
import javax.inject.Inject; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
@WebServlet(name = "SingletonTestServlet", urlPatterns = {"/SingletonTestServlet"}) | |
public class SingletonTestServlet extends HttpServlet { | |
public SingletonTestServlet() { | |
} | |
@Inject | |
MySingletonBean mySingleton; | |
@Override | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
System.out.println("Entered SingletonTestServlet/doGet() on " + new Date().toString() + " . Servlet instance " + this.hashCode() + " Thread : " + Thread.currentThread().getName()); | |
mySingleton.act(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment