Servlet multiple access prevention in my application

below is a URL configured as an autosys job. This invokes the servlet listed below. Can anyone suggest me how to protect this method? psServiceWrapper.processHRFeed (); "called constantly with incorrect data modification every time this URL is clicked, they say it all the time 10 times. I want it to be accessed only one stream at a time.

I know that I need to use a synchronized method or block .. not sure how to do it .. since I am not familiar with threads.

http://mydomain:11000/dorf/HRDORFScriptServlet?script=hrFeed public class HRDORFScriptServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final Logger log = Logger.getLogger(HRDORFScriptServlet.class); private final String script = "script"; @Override protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { // TODO Auto-generated method stub performTask(arg0, arg1); } @Override protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { // TODO Auto-generated method stub performTask(arg0, arg1); } /** Execute the servlet. * * @param request * @param response * @throws ServletException * @throws IOException */ public void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException { DorfUser dorfUser = DorfSessionUtils.getDorfUser(request.getSession()); HRDorfFeedServiceWrapper psServiceWrapper = new HRDorfFeedServiceWrapper(dorfUser); String reqParam = request.getParameter(script); if(reqParam.equals("hrFeed")){ try{ psServiceWrapper.processHRFeed(); } catch(ServiceException se){ log.error("Error While calling HRFeed Service : "+se.getMessage()); } catch(Exception e){ log.error("Error While calling HRFeed Service : "+e.getMessage()); } } } 

}

+4
source share
2 answers

I would move the psServiceWrapper.processHRFeed() functionality to a simple class that implements Runnable.

 public class MyTask implements Runnable { public void run() { psServiceWrapper.processHRFeed(); } } 

Then create an ExecutorService with a fixed thread pool size of 1.

 ExecutorService psServiceRunner = Executors.newFixedThreadPool(1); 

Every time a servlet is called, I send an instance of MyTask for this.

 psServiceRunner.execute(new MyTask()); 

It will be

  • Do not block the servlet caller.
  • make sure that only one servlet can run the method at any given time.
+3
source
 synchronized(this){ psServiceWrapper.processHRFeed(); } 

But this will lead to Bottleneck, since your servlet will stop responding while psServiceWrapper.processHRFeed(); will not be executed by the current thread.

You can also ReetrantLock if using Java 5

Mutual repeated exclusion Lock with the same basic behavior and semantics as the implicit monitor lock, accessible using synchronized methods and operators, but with advanced features.

  private final ReentrantLock lock = new ReentrantLock();//Declare it 

Use it as below

  lock.lock(); // block until condition holds try { psServiceWrapper.processHRFeed(); } finally { lock.unlock() } 
+2
source

Source: https://habr.com/ru/post/1434171/


All Articles