@Test
public void remoteNodeTest() {
WhitePages wp = new WhitePagesImpl();
Grid grid1 = new GridImpl( new HashMap<String, Object>() );
configureGrid1( grid1,
8000,
wp );
Grid grid2 = new GridImpl( new HashMap<String, Object>() );
configureGrid1( grid2,
-1,
wp );
GridNode n1 = grid1.createGridNode( "n1" );
grid1.get( SocketService.class ).addService( "n1", 8000, n1 );
GridServiceDescription<GridNode> n1Gsd = grid2.get( WhitePages.class ).lookup( "n1" );
GridConnection<GridNode> conn = grid2.get( ConnectionFactoryService.class ).createConnection( n1Gsd );
GridNode remoteN1 = conn.connect();
KnowledgeBuilder kbuilder = remoteN1.get( KnowledgeBuilderFactoryService.class ).newKnowledgeBuilder();
Assert.assertNotNull( kbuilder );
String rule = "package test\n"
+ "global org.drools.grid.Grid grid; \n" +
"" +
"rule \"test\""
+ " when"
+ " then"
+ " System.out.println(\"Rule Fired!\");"
+ " end";
kbuilder.add( new ByteArrayResource( rule.getBytes() ),
ResourceType.DRL );
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if ( errors != null && errors.size() > 0 ) {
for ( KnowledgeBuilderError error : errors ) {
System.out.println( "Error: " + error.getMessage() );
}
fail("KnowledgeBase did not build");
}
KnowledgeBaseFactoryService kbfs = remoteN1.get( KnowledgeBaseFactoryService.class );
KnowledgeBase kbase = kbfs.newKnowledgeBase( kbfs.newKnowledgeBaseConfiguration() );
Assert.assertNotNull( kbase );
kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
Assert.assertNotNull( session );
FactHandle handle = session.insert( new MyObject("myObj1") );
Assert.assertNotNull( handle );
int i = session.fireAllRules();
Assert.assertEquals( 1,
i );
grid1.dispose();
grid2.dispose();
assertTrue( n1.isDisposed() );
assertTrue( remoteN1.isDisposed() );
}