Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
Cómo invocar la función Java varias veces desde la expresión Javascript en línea usando ScriptEngine

Estoy evaluando una expresión Javascript en línea usando ScriptEngine de Java 8.

Expresión de ejemplo

 (_tranxAmount > 100) && (profile.received('daily', 'avg', false) < 100)

Donde profile es un objeto de clase Java de vinculación de alcance global y received() es su función local. Evalué con éxito esta expresión usando scriptingEngine.eval(script) y está llamando a profile.received('daily', 'avg', false) Quiero decir functionsManager.received('daily', 'avg', false) también .

El motor de secuencias de comandos no pudo evaluar la expresión de JavaScript si contiene más de una función de Java. En ese caso, solo se llama a la primera función de Java. Cómo resolver este problema?

Por ejemplo

 (_tranxAmount > 100) && (profile.received('daily', 'avg', false) < 100) && (profile.sent('monthly', 'count', false) < 5)

Motor de secuencias de comandos:

 private static ScriptEngine scriptingEngine;

Constructor interno de la clase principal

 scriptingEngine = new ScriptEngineManager().getEngineByName("JavaScript"); scriptingEngine.getBindings(ScriptContext.GLOBAL_SCOPE).put(PROFILING_FUNCTIONS_PREFIX, functionsManager);

Evaluación de la expresión

 Bindings bindings = new SimpleBindings(customMapOfVaraibles); CompiledScript compiled = ((Compilable) scriptingEngine).compile(" (_tranxAmount > 100) && (profile.received('daily', 'avg', false) < 100) && (profile.sent('monthly', 'count', false) < 5)"); compiled .eval(bindings)

Los métodos de functionsManager se ven así

 public BigDecimal received(String intervalType, String statisticsType, boolean considerTranxType) throws InvalidRequirementsException { //Do calculations return calculatedValue; } public BigDecimal sent(String intervalType, String statisticsType, boolean considerTranxType) throws InvalidRequirementsException { //Do calculations return calculatedValue; }
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!