Last active
September 20, 2016 11:27
-
-
Save miere/5649624 to your computer and use it in GitHub Desktop.
Servlet 3.0 sample code found at http://www.javaworld.com/javaworld/jw-02-2009/jw-02-servlet3.htm
This file contains hidden or 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
@WebServlet(name="myServlet", urlPatterns={"/auctionservice"}, asyncSupported=true) | |
public class MyServlet extends HttpServlet { | |
// track bid prices | |
public void doGet(HttpServletRequest request, HttpServletResponse response) { | |
AsyncContext aCtx = request.startAsync(request, response); | |
// This could be a cluser-wide cache. | |
ServletContext appScope = request.getServletContext(); | |
Map<String, List<AsyncContext>> aucWatchers = (Map<String, List<AsyncContext>>)appScope.getAttribute("aucWatchers"); | |
List<AsyncContext> watchers = (List<AsyncContext>)aucWatchers.get(request.getParameter("auctionId")); | |
watchers.add(aCtx); // register a watcher | |
} | |
// place a bid | |
public void doPost(HttpServletRequest request, HttpServletResponse response) { | |
// run in a transactional context | |
// save a new bid | |
AsyncContext aCtx = request.startAsync(request, response); | |
ServletContext appScope = request.getServletContext(); | |
Queue<Bid> aucBids = (Queue<Bid>)appScope.getAttribute("aucBids"); | |
aucBids.add((Bid)request.getAttribute("bid")); // a new bid event is placed queued. | |
} | |
} | |
@WebServletContextListener | |
public class BidPushService implements ServletContextListener{ | |
public void contextInitialized(ServletContextEvent sce) { | |
Map<String, List<AsyncContext>> aucWatchers = new HashMap<String, List<AsyncContext>>(); | |
sce.getServletContext().setAttribute("aucWatchers", aucWatchers); | |
// store new bids not published yet | |
Queue<Bid> aucBids = new ConcurrentLinkedQueue<Bid>(); | |
sce.getServletContext().setAttribute("aucBids", aucBids); | |
Executor bidExecutor = Executors.newCachedThreadPool(); | |
final Executor watcherExecutor = Executors.newCachedThreadPool(); | |
while(true) | |
{ | |
if(!aucBids.isEmpty()) // There are unpublished new bid events. | |
{ | |
final Bid bid = aucBids.poll(); | |
bidExecutor.execute(new Runnable(){ | |
public void run() { | |
List<AsyncContext> watchers = aucWatchers.get(bid.getAuctionId()); | |
for(final AsyncContext aCtx : watchers) | |
{ | |
watcherExecutor.execute(new Runnable(){ | |
public void run() { | |
// publish a new bid event to a watcher | |
aCtx.getResponse().getWriter().print("A new bid on the item was placed. The current price ..., next bid price is ..."); | |
}; | |
}); | |
} | |
} | |
}); | |
} | |
} | |
} | |
public void contextDestroyed(ServletContextEvent sce) { | |
} | |
} |
This file contains hidden or 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
@WebServlet(name="myServlet", urlPatterns={"/slowprocess"}, asyncSupported=true) | |
public class MyServlet extends HttpServlet { | |
public void doGet(HttpServletRequest request, HttpServletResponse response) { | |
AsyncContext aCtx = request.startAsync(request, response); | |
ServletContext appScope = request.getServletContext(); | |
((Queue<AsyncContext>)appScope.getAttribute("slowWebServiceJobQueue")).add(aCtx); | |
} | |
} | |
@WebServletContextListener | |
public class SlowWebService implements ServletContextListener { | |
public void contextInitialized(ServletContextEvent sce) { | |
Queue<AsyncContext> jobQueue = new ConcurrentLinkedQueue<AsyncContext>(); | |
sce.getServletContext().setAttribute("slowWebServiceJobQueue", jobQueue); | |
// pool size matching Web services capacity | |
Executor executor = Executors.newFixedThreadPool(10); | |
while(true) | |
{ | |
if(!jobQueue.isEmpty()) | |
{ | |
final AsyncContext aCtx = jobQueue.poll(); | |
executor.execute(new Runnable(){ | |
public void run() { | |
ServletRequest request = aCtx.getRequest(); | |
// get parameteres | |
// invoke a Web service endpoint | |
// set results | |
aCtx.forward("/result.jsp"); | |
} | |
}); | |
} | |
} | |
} | |
public void contextDestroyed(ServletContextEvent sce) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just curious, what package does implement the annotation
@WebServletContextListener? it is supposed to be @weblistener instead. LINK.