This tutorial will take you through the basics of creating a Fulmine context and creating and subscribing for a record. The tutorial will create two contexts in the same VM; this is purely for simplicity, there is nothing to stop you changing this and creating each context in a separate VM. The full code for this tutorial is here.
IFulmineContext publisherContext = new FulmineContext("HelloWorldPublisher"); publisherContext.setNetwork(new TcpNetwork()); publisherContext.start();
IFulmineContext subscriberContext = new FulmineContext("HelloWorldSubscriber"); subscriberContext.setNetwork(new TcpNetwork(null, 22222)); subscriberContext.start();
subscriberContext.subscribe("HelloWorldPublisher", "helloWorld", ISubscription.WILDCARD_TYPE, ISubscription.WILDCARD_DOMAIN, new ISubscriptionListener() { private final Class<? extends IEvent>[] FILTER = EventListenerUtils.createFilter(IContainer.class); public void addedAsListenerFor(IEventSource source) { // noop } public void removedAsListenerFrom(IEventSource source) { // noop } public Class<? extends IEvent>[] getEventTypeFilter() { // this method is called whenever an event is to be // distributed so we should not be re-creating the array // each time return FILTER; } public void update(IEvent event) { System.out.println("received " + event); } });
The subscribe operation requires the identity of the context to issue the subscription to and the subscription details. The subscription details consist of a regular expression to match against a record identity and the type and domain of the record. In the above example, we provided a subscription composed of a regular expression 'helloWorld', a 'wildcard' type and domain. This will match any record currently or in the future in the publisherContext that has 'helloWorld' as the record identity and any type and domain. A wildcard for the regular expression would be ".*".
publisherContext.getLocalContainer("helloWorld", Type.RECORD, Domain.get(1, "Tutorial"))
try { System.in.read(); } catch (IOException e) { e.printStackTrace(); }
Constructed LogTaskHandler Starting logging executor Started logging executor received Record(helloWorld:Type(10,Record):Domain(1,Tutorial))=[nativeContext=HelloWorldPublisher, state=STALE] received Record(helloWorld:Type(10,Record):Domain(1,Tutorial))=[nativeContext=HelloWorldPublisher, state=LIVE]
The two final output lines show that subscriberContext received the created record and it changed state from STALE to LIVE. The LIVE state means its native context is running and all data in the record is valid. This is the end of this tutorial; it has shown how to create a context, subscribe for a record and how to create a record. The next tutorial will show how to add fields to a record.