[rules-users] Dynamic refresh from Guvnor in Web Application

kappert kappert at hotmail.com
Thu Jan 10 05:25:33 EST 2013


OK, got the file-based version (not from Guvnor) running. The problem was
that the ResourceChangeScannerService must NOT be started. There is a
problem when it tries to load the resource.
But starting the ResourceChangeNotifierService ALONE works!
See function below.

      /**
     * Creates a StatefulKnowledgeSession that uses the specified rules
file.
     * Changes in the rules file are detected on-the-fly (change
notification).
     * <p>
     * TODO A service is started that detects changes in the rules file. It
should be shutdown when an
     * application terminates. This should be imlemented in a production
environment.
     * 
     * @param pathRulesFile String with the location of the rules file. If
your rules file is in a top
     *            directory
     *            (for example src/main/rules), then the path is
"rules.drl". If it is in a package
     *            org.somewhere, then the string is
"org/somewhere/rules.drl".
     * @return StatefulKnowledgeSession
     */
    public static StatefulKnowledgeSession
createStatefulKnowledgeSessionWithFile(String pathRulesFile) {

        ResourceFactory.getResourceChangeNotifierService().start();

        final KnowledgeBuilder kbuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource(pathRulesFile),
ResourceType.DRL);
        final KnowledgeBuilderErrors errors = kbuilder.getErrors();
        if (errors.size() > 0) {
            for (final KnowledgeBuilderError error : errors) {
                System.err.println(error);
            }
            throw new IllegalArgumentException("Could not parse
knowledge.");
        }

        final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

        final KnowledgeAgentConfiguration knowlegdeAgentConfiguration =
KnowledgeAgentFactory
                .newKnowledgeAgentConfiguration();
        //
knowlegdeAgentConfiguration.setProperty("drools.agent.scanResources",
"true"); // not neeeded
        //
knowlegdeAgentConfiguration.setProperty("drools.agent.scanDirectories",
"true"); // not needed
        knowlegdeAgentConfiguration.setProperty("drools.agent.newInstance",
"false"); // required!
        final KnowledgeAgent kagent =
KnowledgeAgentFactory.newKnowledgeAgent("From File", kbase,
                knowlegdeAgentConfiguration);

        return kagent.getKnowledgeBase().newStatefulKnowledgeSession();
    }



--
View this message in context: http://drools.46999.n3.nabble.com/Dynamic-refresh-from-Guvnor-in-Web-Application-tp4021428p4021440.html
Sent from the Drools: User forum mailing list archive at Nabble.com.


More information about the rules-users mailing list