JBPM与OSGi(Apache Felix)、Maven集成及依赖问题咨询
Let's break down your problems and fix them step by step:
1. Why Your Dependencies Aren't Resolving
The main issues here are a mix of outdated API usage and missing critical OSGi integration dependencies:
- Outdated API: In Drools/JBPM 7.x, the old
KnowledgeBase,KnowledgeBuilder, and related classes are deprecated and replaced by the Kie API (e.g.,KieBase,KieBuilder,KieSession). These old classes aren't present in the modern dependencies you're using. - Missing OSGi Integration: You commented out
drools-osgi-integration—this dependency is mandatory for OSGi environments, as it provides the service registry bindings needed to access Drools/JBPM services in Felix. - Version Consistency: While you tried 7.46.0.Final, make sure all your Drools/JBPM dependencies share the exact same version to avoid classpath conflicts.
Here's the corrected Maven dependency setup (using 7.46.0.Final, the latest you tested):
<drools.version>7.46.0.Final</drools.version> <!-- JBPM Core BPMN Support --> <dependency> <groupId>org.jbpm</groupId> <artifactId>jbpm-bpmn2</artifactId> <version>${drools.version}</version> <scope>provided</scope> </dependency> <!-- Drools Core & Compiler --> <dependency> <groupId>org.drools</groupId> <artifactId>drools-core</artifactId> <version>${drools.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-compiler</artifactId> <version>${drools.version}</version> <scope>provided</scope> </dependency> <!-- Decision Table Support --> <dependency> <groupId>org.drools</groupId> <artifactId>drools-decisiontables</artifactId> <version>${drools.version}</version> <scope>provided</scope> </dependency> <!-- Kie API (Replaces old Drools API) --> <dependency> <groupId>org.kie</groupId> <artifactId>kie-api</artifactId> <version>${drools.version}</version> <scope>provided</scope> </dependency> <!-- Critical OSGi Integration Dependencies --> <dependency> <groupId>org.drools</groupId> <artifactId>drools-osgi-integration</artifactId> <version>${drools.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.jbpm</groupId> <artifactId>jbpm-osgi</artifactId> <version>${drools.version}</version> <scope>provided</scope> </dependency>
- Use
<scope>provided</scope>because these dependencies should be deployed as separate bundles in your Felix container, not packaged into your project bundle (avoids classloader conflicts). - Ensure all these bundles are deployed to Felix before your application bundle—you can download them from Maven Central or use Felix's OBR repository for automatic deployment.
2. Correct OSGi Integration to Call JBPM/BPMN/Decision Tables
Forget the old KnowledgeBase-based code—Drools/JBPM 7.x uses the Kie API, and OSGi integration requires using the OSGi service registry to access Kie services properly. Here's how to implement it:
Step 1: Retrieve Kie OSGi Services
In an OSGi component or activator, use a ServiceTracker to safely get the KieOSGiServices from the registry:
import org.kie.osgi.api.KieOSGiServices; import org.osgi.framework.BundleContext; import org.osgi.util.tracker.ServiceTracker; // Get your bundle context (from Activator or OSGi component injection) BundleContext bundleContext = ...; // Track the KieOSGiServices service with a 5-second timeout ServiceTracker<KieOSGiServices, KieOSGiServices> kieServiceTracker = new ServiceTracker<>(bundleContext, KieOSGiServices.class, null); kieServiceTracker.open(); KieOSGiServices kieOSGiServices = kieServiceTracker.waitForService(5000); if (kieOSGiServices == null) { throw new RuntimeException("Failed to find KieOSGiServices in OSGi registry—ensure drools-osgi-integration bundle is deployed"); }
Step 2: Load BPMN/Decision Table Resources
Use the Kie API to load your process/decision table files:
import org.kie.api.KieServices; import org.kie.api.builder.KieBuilder; import org.kie.api.builder.KieFileSystem; import org.kie.api.builder.KieRepository; import org.kie.api.runtime.KieContainer; KieServices kieServices = kieOSGiServices.getKieServices(); KieRepository kieRepository = kieServices.getRepository(); KieFileSystem kfs = kieServices.newKieFileSystem(); // Add your BPMN file (replace path with your actual file) kfs.write(kieServices.getResources().newClassPathResource("MyProcess.bpmn", getClass())); // Add your decision table (Excel file) kfs.write(kieServices.getResources().newClassPathResource("MyDecisionTable.xls", getClass())); // Build the KieModule and check for errors KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll(); if (kieBuilder.getResults().hasMessages(org.kie.api.builder.Message.Level.ERROR)) { throw new RuntimeException("Failed to build Kie module: " + kieBuilder.getResults().getMessages()); } // Create a KieContainer to access the deployed assets KieContainer kieContainer = kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
Step 3: Execute Processes or Decision Tables
Now you can run your BPMN process or execute decision table rules:
import org.kie.api.runtime.KieSession; // For BPMN Process Execution KieSession ksession = kieContainer.newKieSession(); // Start the process (replace with your process ID from the BPMN file) ksession.startProcess("com.yourcompany.MyProcess"); // For Decision Table Execution (rules) // Insert your fact object (the data your decision table evaluates) YourBusinessFact fact = new YourBusinessFact(); fact.setInputValue("test"); ksession.insert(fact); // Fire all rules from the decision table ksession.fireAllRules(); // Always dispose the session when done to free resources ksession.dispose();
Step 4: Configure Your OSGi Bundle
Make sure your bundle's MANIFEST.MF imports the required packages. If using Maven Bundle Plugin, add this to your build configuration:
<build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>4.2.1</version> <extensions>true</extensions> <configuration> <instructions> <Import-Package> org.kie.api.*, org.kie.api.builder.*, org.kie.api.runtime.*, org.kie.osgi.api.*, org.osgi.framework, org.osgi.util.tracker, * </Import-Package> <Export-Package>com.yourcompany.jbpm.integration.*</Export-Package> </instructions> </configuration> </plugin> </plugins> </build>
Quick Tips
- Deploy Dependencies First: Ensure all Drools/JBPM OSGi bundles are deployed to Felix before your application bundle.
- Avoid Deprecated API: Stick to the Kie API to maintain compatibility with future Drools/JBPM versions.
- Decision Table Format: Double-check your Excel decision table follows Drools' format rules (correct headers, rule columns, etc.) to avoid execution errors.
内容的提问来源于stack exchange,提问作者Tiya

