Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6338569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:28:20+00:00 2026-05-24T19:28:20+00:00

I develop an application using the Play! Framework which makes heavy use of the

  • 0

I develop an application using the Play! Framework which makes heavy use of the javax.script Package, including the ScriptEngine. As ScriptEngines are expensive to create and it would make sense to use them across multiple requests (I don’t bother to create multiple ScriptEngines, one per Thread – at least i won’t create ScriptEngines for each Request over and over).

I think this case is not restriced to ScriptEngines, there might be something in the framework I’m not aware of to handle such cases.

Thank you for any ideas you have!
Malax

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:28:20+00:00Added an answer on May 24, 2026 at 7:28 pm

    Play is stateless, so there is no “session-like” mechanism to link an object to a user. You may have 2 alternatives:

    Use the Cache. Store the ScriptEngine in the cache with a unique ID, and add a method that checks if it’s still there. Something like:

    public static Engine getScriptEngine(Long userId) {
         String key = "MY_ENGINE" + userId;
         ScriptEngine eng = (ScriptEngine) Cache.get(key);
         if(eng == null) {
            eng = ScriptEngine.create(); 
            Cache.put(key, eng);
         }
         return eng;
    }
    

    Or create a singleton object that contains a static instance of the ScriptEngine so it’s always there once the server starts.

    I would say the Cache one is the best approach.

    EDIT: on your comment, this will depend on situation:

    1. If you want to reuse a Engine across multiple request of a unique user (that is, each user has his own ScriptEngine to work with) the cache method works as the cache links the Engine to the user id. This would solve any threading issue too.
    2. Otherwise, if you want to reuse it across multiple requests of multiple users, the static method is a better approach. But as you mention the access won’t be thread safe, in Play or in any system.

    I’m thinking your best bet is to work asynchronously with them. I don’t know how you will use the ScriptEngines, but try to do something like this:

    • On request, store an entry in a table from the db marking a ScriptEngine processing request
    • In the same request, launch an asynchronous job (or have on running every 30 seconds)
    • The job will read the first entry of the table, remove it, do the task, return answer to the user. The job may have a pool of ScriptEngine to work with.
    • As jobs are not launched again while a current job is working, if you have enought requests the job will never cease working. If it does it means that you don’t need engines at that time, and they will be recreated on demand.

    This way you work linearly with a pool, ignoring threading issues. If you can’t do this, then you need to fix the thread-safety of your ScriptEngine, as you can’t pretend to share an object that it’s not thread safe in a server environemnt which spawns multiple threads 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I intend to develop an application using .Net 4.0, and intend to use EF4
I am trying to develop an iphone application using sencha framework .I need to
I an trying to develop a windows application using C# that can play streamed
I want to develop my application using google app engine into which I will
Is there free framework for develop andoroid application using .net (c# / VB) ?
How to develop modular application using Zend framework?
I have to develop an application using C#.net that has to be run once
I'm starting to develop an application using MySQL and although I've developed apps before
I am new to Swing. I am trying to develop an application using Swing.
I want to develop an iPhone application using the utility template, where the flip

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.