JBoss.org Community Documentation

- Prev
- Next

## Chapter 10. CDI

## 10.1. Introduction

CDI, Contexts and Dependency Injection, is java specification that provides declarative controls and strucutres to an application. KIE can use it to automatically instantiate and bind things, without the need to use the programmatic api.

## 10.2. Annotations

@KContainer, @KBaser and @KSession all support an optional 'name' attribute. CDI typically does "getOrCreate" when it injects, all injections receive the same instance for the same set of annotations. the 'name' annotation forces a unique instance for each name, although all intsance for that name will be identity equals.

### 10.2.1. @KRealaseId

Used to bind an instance to a specific version of a KieModule. If kie-ci is on the classpath this will resolve dependencies automatically, downloading from remote repositories.

### 10.2.2. @KContainer

@KContainer is optional as it can be detected and added by the use of @Inject and variable type inferrence.

    @Inject

    private KieContainer kContainer;

Figure 10.1. Injects Classpath KieContainer

    @Inject

    @KReleaseId(groupId = "jar1", artifactId = "art1", version = "1.1")

    private KieContainer kContainer;

Figure 10.2. Injects KieContianer for Dyanmic KieModule

    @Inject

    @KContainer(name = "kc1")

    @KReleaseId(groupId = "jar1", artifactId = "art1", version = "1.1")

    private KieContainer kContainer;

Figure 10.3. Injects named KieContianer for Dyanmic KieModule

### 10.2.3. @KBase

@KBase is optional as it can be detected and added by the use of @Inject and variable type inferrence.

The default argument, if given, maps to the value attribute and specifie the name of the KieBase from the kmodule.xml file.

    @Inject

    private KieBase kbase;

Figure 10.4. Injects the Default KieBase from the Classpath KieContainer

    @Inject

    @KReleaseId( groupId = "jar1", artifactId = "art1", version = "1.0")

    private KieBase kbase;

Figure 10.5. Injects the Default KieBase from a Dynamic KieModule

    @Inject

    @KSession("kbase1")

    @KReleaseId( groupId = "jar1", rtifactId = "art1", version = "1.0")

    private KieBase kbase1v10;

    @Inject

    @KBase("kbase1")

    @KReleaseId( groupId = "jar1", rtifactId = "art1", version = "1.1")

    private KieBase kbase1v10;

Figure 10.6. Side by side version loading for 'jar1.KBase1' KieBase

    @Inject

    @KSession(value="kbase1", name="kb1")

    @KReleaseId( groupId = "jar1", artifactId = "art1", version = "1.0")

    private KieBase kbase1kb1;

    @Inject

    @KSession(value="kbase1", name="kb2")

    @KReleaseId( groupId = "jar1", artifactId = "art1", version = "1.0")

    private KieBase kbase1kb2;

Figure 10.7. Use 'name' attribute to force new Instance for 'jar1.KBase1' KieBase

### 10.2.4. @KSession for KieSession

@KSession is optional as it can be detected and added by the use of @Inject and variable type inferrence.

The default argument, if given, maps to the value attribute and specifie the name of the KieSession from the kmodule.xml file

    @Inject

    private KieSession ksession;

Figure 10.8. Injects the Default KieSession from the Classpath KieContainer

    @Inject

    @KReleaseId( groupId = "jar1", artifactId = "art1", version = "1.0")

    private KieSession ksession;

Figure 10.9. Injects the Default KieSession from a Dynamic KieModule

    @Inject

    @KSession("ksession1")

    @KReleaseId( groupId = "jar1", rtifactId = "art1", version = "1.0")

    private KieSession ksessionv10;

    @Inject

    @KSession("ksession1")

    @KReleaseId( groupId = "jar1", rtifactId = "art1", version = "1.1")

    private KieSession ksessionv11;

Figure 10.10. Side by side version loading for 'jar1.KBase1' KieBase

    @Inject

    @KSession(value="ksession1", name="ks1")

    @KReleaseId( groupId = "jar1", artifactId = "art1", version = "1.0")

    private KieSession ksession1ks1

    @Inject

    @KSession(value="ksession1", name="ks2")

    @KReleaseId( groupId = "jar1", artifactId = "art1", version = "1.0")

    private KieSession ksession1ks2

Figure 10.11. Use 'name' attribute to force new Instance for 'jar1.KBase1' KieSession

### 10.2.5. @KSession for StatelessKieSession

@KSession is optional as it can be detected and added by the use of @Inject and variable type inferrence.

The default argument, if given, maps to the value attribute and specifie the name of the KieSession from the kmodule.xml file.

    @Inject

    private StatelessKieSession ksession;

Figure 10.12. Injects the Default StatelessKieSession from the Classpath KieContainer

    @Inject

    @KReleaseId( groupId = "jar1", artifactId = "art1", version = "1.0")

    private StatelessKieSession ksession;

Figure 10.13. Injects the Default StatelessKieSession from a Dynamic KieModule

    @Inject

    @KSession("ksession1")

    @KReleaseId( groupId = "jar1", rtifactId = "art1", version = "1.0")

    private StatelessKieSession ksessionv10;

    @Inject

    @KSession("ksession1")

    @KReleaseId( groupId = "jar1", rtifactId = "art1", version = "1.1")

    private StatelessKieSession ksessionv11;

Figure 10.14. Side by side version loading for 'jar1.KBase1' KieBase

    @Inject

    @KSession(value="ksession1", name="ks1")

    @KReleaseId( groupId = "jar1", artifactId = "art1", version = "1.0")

    private StatelessKieSession ksession1ks1

    @Inject

    @KSession(value="ksession1", name="ks2")

    @KReleaseId( groupId = "jar1", artifactId = "art1", version = "1.0")

    private StatelessKieSession ksession1ks2

Figure 10.15. Use 'name' attribute to force new Instance for 'jar1.KBase1' StatelessKieSession

## 10.3. API Example Comparison

CDI can inject instances into fields, or even pass them as arguments. In this example field injection is used.

    @Inject

    @KSession("ksession1")

    KieSession kSession;

    public void go(PrintStream out) {

        kSession.setGlobal("out", out);

        kSession.insert(new Message("Dave", "Hello, HAL. Do you read me, HAL?"));

        kSession.fireAllRules();

    }

Figure 10.16. CDI example for a named KieSession

This is less code and more declarative than the api approach.

    public void go(PrintStream out) {

        KieServices ks = KieServices.Factory.get();

        KieContainer kContainer = ks.getKieClasspathContainer();

        KieSession kSession = kContainer.newKieSession("ksession1");

        kSession.setGlobal("out", out);

        kSession.insert(new Message("Dave", "Hello, HAL. Do you read me, HAL?"));

        kSession.fireAllRules();

    }

Figure 10.17. API equivalent example for a named KieSession

- PrevChapter 9. Drools Commands
- Top of page
- Front page
- NextChapter 11. Integration with Spring