Java Dynamic Execution computation expression weapon--scriptengine

Source: Internet
Author: User

In the configuration file for the configuration of the system parameters, sometimes need to better understand the specific meaning of the parameter values, often in the form of calculation expressions, such as 1 days into seconds to 86400, if written 24 * 60 * 60 is a clear expression is the number of seconds per day. However, this expression is obtained as a string by the properties, and this expression needs to be evaluated dynamically.

ScriptEngine This object is specifically designed to handle dynamic execution expressions, primarily calling its Eval method to execute dynamically (similar to the Eval method in JavaScript), which returns a result of object, and the return result for a computed expression is a double type. So here we first convert the object to a double and then cast it according to the actual need.

The configuration file is as follows:

#标准缓存时长 (unit: seconds) #默认为1天 (24 * 60 * 60) or 86400STD_CACHE_TIME_OUT=24 * 60 * 60

Its usage is as follows:

Scriptenginemanager _mgr = new Scriptenginemanager ();//Only JavaScript actuators are called here, and JavaScript scripts can be executed, Other types of scripts can also be called scriptengine _enginer = _mgr.getenginebyname ("javascript"); try{String expression =   Properties.getproperty ("Std_cache_time_out");    Object result = _enginer.eval (expression);   _max_cache_time_out = (long) double.parsedouble (result.tostring ());  }}catch (Scriptexception e) {e.printstacktrace (); }

See the ScriptEngine using the API with the method provided by the ScriptEngine interface

This article from "Good memory than bad writing" blog, please be sure to keep this source http://gytian.blog.51cto.com/1065457/1604847

Java Dynamic Execution computation expression weapon--scriptengine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.