Problem while performing asynchronous tasks using ExecutorService

I previously asked a question regarding the initialization of ExecutorService and Apache Velocity. To give a brief summary, I have a Java EE interface that accepts user requests, and then uses ExecutorService (SingleThreadedExecutor installed as a daemon) for each of these requests to start a lengthy workflow. This workflow is contained in the library and works well and, as expected, when launched offline through an eclipse. When I called from the website (servlet), I noticed that the workflows hung sequentially at the point where the Velocity engine (Velocity.init () or ve.init ()) was initialized. Hence my above question.

When none of the answers / suggestions worked, I assumed that this was due to how Velocity was launched and decided to switch to FreeMarker. Now I see that the workflow hanged in the same place for implementing FreeMarker. This "place" is the mail part that evaluates the template against the fist of the transmitted data objects and returns the mail string. The class that calls the Freemark'ing class and the FreeMark class is as follows:

public class mailBuilder { private static final Logger log = Logger.getLogger( mailBuilder.class ); static String a; static String b; public mailBuilder(CustomDataStructure input) { a = input.getA(); b = input.getB(); } public static String returnMailstring() throws Exception { log.info("Gathering elements to construct email."); String mailText=null; Map context = new HashMap(); context.put("a",a); context.put("b",b); log.info("Calling Freemarker"); mailText=FreeMarkIT.ReturnReportString(context); log.info("Freeemarker returned string"); return mailText; } } 

The FreeMarkIT class is as follows:

  public class FreeMarkIT { private static final Logger log = Logger.getLogger( FreeMarkIT.class ); private static Configuration config; private static Template template; public static String ReturnReportString(Map model) throws IOException, TemplateException { StringWriter sw = new StringWriter(); try { log.info("Going to get the template"); config= new Configuration(); log.info("Now really"); template=config.getTemplate("src/resource/email_template.vm"); log.info("Done initializing template"); template.process(model, sw); sw.flush(); } catch(Exception e) { System.out.println(e.getMessage()); } return sw.getBuffer().toString(); } } 

Now, from my logging, it looks like the worker thread is hanging on the line config=new Configuration()

Again, this works as expected offline when starting from eclipse, but still hangs when called from a servlet using ExecutorService.

Im starting to think / understand that this has nothing to do with either Velocity or FreeMarker, and something has to do with ExecutorService. Any advice or suggestions would be of great help.

thanks

0
source share
1 answer

your code is not thread safe, since you share config and template with all thread instances (and constantly reinstall them). the easiest way to make it thread safe would be to make local config and template variables in the method instead of static members. as @JBNizet noted in the comments, you have a similar problem in mailBuilder with a and b . you can first check out some tutorials on the basics of object-oriented programming, and then return to this problem (hint, in general, you should avoid static member variables except constants).

+3
source

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


All Articles