[rules-users] StatefulKnowledgeSession leaves threads running

Rafael Ribeiro rafaelri at gmail.com
Thu Jul 16 13:10:18 EDT 2009


Hi Edson!

 as I've already mentioned I've modified the sample so, if you get the
StockTick sample and replace the Main class this will run fine.
 The sample shuts down fine but as I saw it sets the JFrame uses
frame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE ); which in
turn makes the trick.

 Below the code of my modified main class:

package org.drools.examples.broker;


import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseConfiguration;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.conf.EventProcessingOption;
import org.drools.examples.broker.model.Company;
import org.drools.examples.broker.model.CompanyRegistry;
import org.drools.examples.broker.model.StockTick;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.rule.WorkingMemoryEntryPoint;

public class Main {
    private static final String RULES_FILE = "/broker.drl";
    static StatefulKnowledgeSession session;
    static WorkingMemoryEntryPoint tickStream;

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        // set up and show main window

        CompanyRegistry companies = new CompanyRegistry();

        KnowledgeBuilder builder =
KnowledgeBuilderFactory.newKnowledgeBuilder();
        try {
            builder.add( ResourceFactory.newInputStreamResource(
Main.class.getResourceAsStream( RULES_FILE ) ),
                         ResourceType.DRL);
        } catch ( Exception e ) {
            e.printStackTrace();
        }
        if( builder.hasErrors() ) {
            System.err.println(builder.getErrors());
            System.exit( 0 );
        }

        KnowledgeBaseConfiguration conf =
KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
        conf.setOption( EventProcessingOption.STREAM );
        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase( conf );
        kbase.addKnowledgePackages( builder.getKnowledgePackages() );

        session = kbase.newStatefulKnowledgeSession();

        session.setGlobal( "services", new BrokerServices() {
            public void log(String message) {
                System.out.println(message);
            }
        });

        for( Company company : companies.getCompanies() ) {
            session.insert( company );
        }
        session.fireAllRules();

        tickStream = session.getWorkingMemoryEntryPoint( "StockTick stream"
);

        for (int i=10;i>0;i--) {
            tickStream.insert(new StockTick("RHT", i*10,
System.currentTimeMillis()));
            session.getAgenda().getAgendaGroup( "evaluation" ).setFocus();
            session.fireAllRules();
        }
        session.dispose();
        session.halt();
        System.out.println("Still running...");
    }

}


regards,
Rafael Ribeiro

2009/7/16 Edson Tirelli <tirelli at post.com>

>
>    Hi Rafael,
>
>    Are you setting the MultithreadEvaluation option or is this using
> default options? Do you have an example code that shows this behavior?
>
>    Thanks,
>      Edson
>
> 2009/7/16 Rafael Ribeiro <rafaelri at gmail.com>
>
>>
>> Hi all,
>>
>>  I've downloaded Drools fusion sample and started to make some changes to
>> the code (first of all wipe out the UI so I can test it easier).
>>  I tried to get to a minimal set so I can run a console main class and see
>> what happens but I am facing a strange behaviour.
>>  As soon as I start pushing events into the WorkingMemoryEntryPoint
>> ("StockTick stream") a Thread - not daemonized since it blocks shutdown
>> from
>> ending - is spawned and this prevents my JVM from shutting down (obviously
>> if I call System.exit(0) it will but I am avoiding this).
>>  I've tried both to halt and dispose the session that this entry point
>> belongs but with no success. Does anyone know how could I get rid of this
>> thread?
>>
>> best regards,
>> --
>>
>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> http://lists.jboss.org/mailman/listinfo/rules-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20090716/37beb2a7/attachment.html 


More information about the rules-users mailing list