Search in sources :

Example 1 with StringConsumer

use of com.robo4j.StringConsumer in project robo4j by Robo4J.

the class RemoteContextTests method buildReceiverSystemStringConsumer.

/*
	 * Builds a system ready to receive strings.
	 */
private RoboContext buildReceiverSystemStringConsumer() throws RoboBuilderException, ConfigurationException {
    RoboBuilder builder = new RoboBuilder(SystemUtil.getInputStreamByResourceName("testRemoteReceiver.xml"));
    Configuration configuration = new ConfigurationBuilder().addInteger("totalNumberMessages", 1).build();
    StringConsumer stringConsumer = new StringConsumer(builder.getContext(), UNIT_STRING_CONSUMER);
    stringConsumer.initialize(configuration);
    builder.add(stringConsumer);
    RoboContext receiverSystem = builder.build();
    receiverSystem.start();
    return receiverSystem;
}
Also used : ConfigurationBuilder(com.robo4j.configuration.ConfigurationBuilder) Configuration(com.robo4j.configuration.Configuration) RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) StringConsumer(com.robo4j.StringConsumer)

Example 2 with StringConsumer

use of com.robo4j.StringConsumer in project robo4j by Robo4J.

the class RemoteContextTests method messageToDiscoveredContextTest.

@Test
void messageToDiscoveredContextTest() throws RoboBuilderException, IOException, ConfigurationException {
    RoboBuilder builder = new RoboBuilder(SystemUtil.getInputStreamByResourceName("testRemoteMessageReceiverSystem.xml"));
    StringConsumer consumer = new StringConsumer(builder.getContext(), ACK_CONSUMER);
    builder.add(consumer);
    RoboContext receiverCtx = builder.build();
    receiverCtx.start();
    // Note that all this cludging about with local lookup service
    // implementations etc would normally not be needed.
    // This is just to isolate this test from other tests.
    final LocalLookupServiceImpl localLookup = new LocalLookupServiceImpl();
    final LookupService service = LookupServiceTests.getLookupService(localLookup);
    LookupServiceProvider.setDefaultLookupService(service);
    service.start();
    for (int i = 0; i < NUMBER_ITERATIONS && (service.getDescriptor("7") == null); i++) {
        SystemUtil.sleep(200);
    }
    assertTrue(service.getDiscoveredContexts().size() > 0);
    RoboContextDescriptor descriptor = service.getDescriptor("7");
    assertNotNull(descriptor);
    builder = new RoboBuilder(RemoteContextTests.class.getClassLoader().getResourceAsStream("testMessageEmitterSystem_10.xml"));
    RemoteStringProducer remoteStringProducer = new RemoteStringProducer(builder.getContext(), REMOTE_UNIT_EMITTER);
    remoteStringProducer.initialize(getEmitterConfiguration("7", ACK_CONSUMER));
    builder.add(remoteStringProducer);
    RoboContext emitterContext = builder.build();
    localLookup.addContext(emitterContext);
    emitterContext.start();
    remoteStringProducer.sendMessage("sendRandomMessage");
    for (int i = 0; i < NUMBER_ITERATIONS && consumer.getReceivedMessages().size() == 0; i++) {
        SystemUtil.sleep(200);
    }
    assertTrue(consumer.getReceivedMessages().size() > 0);
    System.out.println("Got messages: " + consumer.getReceivedMessages());
    emitterContext.shutdown();
    receiverCtx.shutdown();
}
Also used : RoboBuilder(com.robo4j.RoboBuilder) RoboContext(com.robo4j.RoboContext) StringConsumer(com.robo4j.StringConsumer) Test(org.junit.jupiter.api.Test)

Aggregations

RoboBuilder (com.robo4j.RoboBuilder)2 RoboContext (com.robo4j.RoboContext)2 StringConsumer (com.robo4j.StringConsumer)2 Configuration (com.robo4j.configuration.Configuration)1 ConfigurationBuilder (com.robo4j.configuration.ConfigurationBuilder)1 Test (org.junit.jupiter.api.Test)1