Search code examples
javaspringdrools

How can I set global variable in drools using drools-spring and use same in DRL file


Basically I want to store a class reference as global variable from spring service class and use the same inside the DRL file to access method and properties of global reference.


Solution

  • Inside your service class use below code:

    StatelessKnowledgeSession knowledgeSession;
    CustomClass reference;
    knowledgeSession.setGlobal("global1", reference);
    knowledgeSession.execute(fact);
    

    Inside DRL file

    import CustomClass;
    
    global CustomClass global1
    

    Use reference inside any rule.