GATE Applications as Web Services Ian Roberts. University of Sheffield NLP Introduction Scenario:  Implementing a web service (or other web application)

Slides:



Advertisements
Similar presentations
Chapter 6 Server-side Programming: Java Servlets
Advertisements

Internet i jego zastosowania 1 J2EE Servlets. Internet i jego zastosowania 2 Agenda Overview Servlet Interface Servlet Context Request Response Sample.
Introduction to Java 2 Programming Lecture 10 Applets.
University of Sheffield NLP GATE development hints Reporting bugs Submitting a patch The user guide Continuous integration.
Servlets, JSP and JavaBeans Joshua Scotton.  Getting Started  Servlets  JSP  JavaBeans  MVC  Conclusion.
Apache Struts Technology
Designing a thread-safe class  Store all states in public static fields  Verifying thread safety is hard  Modifications to the program hard  Design.
Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)
MC365 Application Servers: Servlets. Today We Will Cover: What a servlet is The HTTPServlet and some of its more important methods How to configure the.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Object-Oriented Enterprise Application Development Tomcat 3.2 Configuration Last Updated: 03/30/2001.
An introduction to Java Servlet Programming
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
28/1/2001 Seminar in Databases in the Internet Environment Introduction to J ava S erver P ages technology by Naomi Chen.
Threads - Definition - Advantages using Threads - User and Kernel Threads - Multithreading Models - Java and Solaris Threads - Examples - Definition -
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
Server Architecture Models Operating Systems Hebrew University Spring 2004.
A. Frank - P. Weisberg Operating Systems Introduction to Tasks/Threads.
Q: According to Intel, the Pentium conforms to the IEEE standards 754 and 854 for floating point arithmetic. If you fly in aircraft designed using a Pentium,
Session-02.
Database Management Systems (DBMS)
SE-2840 Dr. Mark L. Hornick1 Java Servlet-based web apps Servlet Architecture.
UNIT-V The MVC architecture and Struts Framework.
M. Taimoor Khan * Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic,
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Java Servlets CS-422. Application Mapping Your servlet application will be mapped to a directory structure: –“myapp” maps to some directory C:/docs/apps/myapp.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
Design Patterns Phil Smith 28 th November Design Patterns There are many ways to produce content via Servlets and JSPs Understanding the good, the.
 Embeds Java code  In HTML tags  When used well  Simple way to generate dynamic web-pages  When misused (complex embedded Java)  Terribly messy.
Chapter 5 Being a Web App. Very few servlet or JSP stands alone Many times in our application, different servlets or JSPs need to share information 
SB Implementing ScriptBasic Multi- Thread How to embed ScriptBasic multi-thread?
Operating Systems Lecture 2 Processes and Threads Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of.
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–
Chapter 3 Servlet Basics. 1.Recall the Servlet Role 2.Basic Servlet Structure 3.A simple servlet that generates plain text 4.A servlet that generates.
Webcommerce Computer Networks Webcommerce by Linnea Reppa Douglas Martindale Lev Shalevich.
Running Servlets JSDK2.1 default.cfg : Web Server configuration information batch files to start and stop server Servlet properties in /webpages/WEB-INF.
® IBM Software Group © 2007 IBM Corporation Servlet Listeners
Java Servlets & Java Server Pages Lecture July 2013.
1 (Worker Queues) cs What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.
Introduction to GATE Developer Ian Roberts. University of Sheffield NLP Overview The GATE component model (CREOLE) Documents, annotations and corpora.
Threading Servlets Can handle multiple clients concurrently Shared resources must be synchronized or create a servlet that handles one request at a time.
@2008 Huynh Ngoc Tin Chapter #2 JAVA SERVLET PRGRAMMING.
INTEGRATION OF BACKBONE.JS WITH SPRING 3.1. Agenda New Features and Enhancements in Spring 3.1 What is Backbone.js and why I should use it Spring 3.1.
SE-2840 Dr. Mark L. Hornick1 Servlet Threads and Sessions.
Copyright © 2002 ProsoftTraining. All rights reserved. Java Servlets.
1 Java Servlets l Servlets : programs that run within the context of a server, analogous to applets that run within the context of a browser. l Used to.
Java Enterprise Edition Programming Page 1 of 9Configuring Servlets Web Application Context Name  In multiple web applications, a “context name” is used.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, written in Java code, that.
Threads. Readings r Silberschatz et al : Chapter 4.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
The Object Pool Pattern (Creational – Not a GoF Pattern) ©SoftMoore ConsultingSlide 1.
Text INTRODUCTION TO ASP.NET. InterComm Campaign Guidelines CONFIDENTIAL Simply Server side language Simplified page development model Modular, well-factored,
1 Validation SAX-DOM. Objectives 2  Schema Validation Framework  XML Validation After Transformation  Workshops.
CSC Java Programming, Fall, 2008 August 28, 2008, class 2 Tuesday, September 2 uses Monday’s schedule!
1 Introduction to Threads Race Conditions. 2 Process Address Space Revisited Code Data OS Stack (a)Process with Single Thread (b) Process with Two Threads.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
CS-4220 Dr. Mark L. Hornick1 Single class doing too many things =Bad Code Smell.
CS3220 Web and Internet Programming Introduction to Java Servlets
GATE Applications as Web Services
Servlet Sessions and Cookies
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
Introduction to J2EE Architecture
Servlet API and Lifecycle
Java Web Application Framework
CS3220 Web and Internet Programming Introduction to Java Servlets
CS510 Operating System Foundations
CS Introduction to Operating Systems
Presentation transcript:

GATE Applications as Web Services Ian Roberts

University of Sheffield NLP Introduction Scenario:  Implementing a web service (or other web application) that uses GATE Embedded to process requests.  Want to support multiple concurrent requests  Long running process - need to be careful to avoid memory leaks, etc. Example used is a plain HttpServlet  Principles apply to other frameworks (struts, Spring MVC, Metro/CXF, Grails…)

University of Sheffield NLP Setting up GATE libraries in WEB-INF/lib  gate.jar + JARs from lib Usual GATE Embedded requirements:  A directory to be "gate.home"  Site and user config files  Plugins directory  Call Gate.init() once (and only once) before using any other GATE APIs

University of Sheffield NLP Initialisation using a ServletContextListener ServletContextListener is registered in web.xml Called when the application starts up public void contextInitialized(ServletContextEvent e) { ServletContext ctx = e.getServletContext(); File gateHome = new File(ctx.getRealPath("/WEB-INF")); Gate.setGateHome(gateHome); File userConfig = new File(ctx.getRealPath("/WEB-INF/user.xml")); Gate.setUserConfigFile(userConfig); // site config is gateHome/gate.xml // plugins dir is gateHome/plugins Gate.init(); } gate.web..example.GateInitListener

University of Sheffield NLP GATE in a multithreaded environment GATE PRs are not thread-safe  Due to design of parameter-passing as JavaBean properties Must ensure that a given PR/Controller instance is only used by one thread at a time

University of Sheffield NLP First attempt: one instance per request Naïve approach - create new PRs for each request public void doPost(request, response) { ProcessingResource pr = Factory.createResource(...); try { Document doc = Factory.newDocument(getTextFromRequest(request)); try { // do some stuff } finally { Factory.deleteResource(doc); } finally { Factory.deleteResource(pr); } Many levels of nested try/finally: ugly but necessary to make sure we clean up even when errors occur. You will get very used to these…

University of Sheffield NLP Problems with this approach Guarantees no interference between threads But inefficient, particularly with complex PRs (large gazetteers, etc.) Hidden problem with JAPE:  Parsing a JAPE grammar creates and compiles Java classes  Once created, classes are never unloaded  Even with simple grammars, eventually OutOfMemoryError (PermGen space)

University of Sheffield NLP Second attempt: using ThreadLocals Store the PR/Controller in a thread local variable private ThreadLocal controller = new ThreadLocal () { protected CorpusController initialValue() { return loadController(); } }; private CorpusController loadController() { //... } public void doPost(request, response) { CorpusController c = controller.get(); // do stuff with the controller }

University of Sheffield NLP Better than attempt 1… Only initialise resources once per thread Interacts nicely with typical web server thread pooling But if a thread dies, no way to clean up its controller  Possibility of memory leaks

University of Sheffield NLP A solution: object pooling Manage your own pool of Controller instances Take a controller from the pool at the start of a request, return it (in a finally!) at the end Number of instances in the pool determines maximum concurrency level

University of Sheffield NLP Simple example private BlockingQueue pool; public void init() { pool = new LinkedBlockingQueue (); for(int i = 0; i < POOL_SIZE; i++) { pool.add(loadController()); } public void doPost(request, response) { CorpusController c = pool.take(); try { // do stuff } finally { pool.add(c); } public void destroy() { for(CorpusController c : pool) Factory.deleteResource(c); } Blocks if the pool is empty: use poll() if you want to handle empty pool yourself

University of Sheffield NLP Further reading Spring Framework   Handles application startup and shutdown  Configure your business objects and connections between them using XML  GATE provides helpers to initialise GATE, load saved applications, etc.  Built-in support for object pooling  Web application framework (Spring MVC)  Used by other frameworks (Grails, CXF, …)

University of Sheffield NLP Conclusions Only use GATE Resources in one thread at a time Make sure to clean up after yourself, even when things go wrong  try/finally  Whenever you createResource, be sure to deleteResource