Fulmine "Hello World" Tutorial

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.

  • Create the first context; the publisher that will create the record
            IFulmineContext publisherContext = new FulmineContext("HelloWorldPublisher");
            publisherContext.setNetwork(new TcpNetwork());
            publisherContext.start();
    
  • Create the second context; the subscriber that will subscribe for the record from the publisher (note that we provide a TCP port this time as the default one was used by the previous context).
            IFulmineContext subscriberContext = new FulmineContext("HelloWorldSubscriber");
            subscriberContext.setNetwork(new TcpNetwork(null, 22222));
            subscriberContext.start();
    
  • Set up a subscription in the subscriber context for a 'helloWorld' record from the publisher context.
            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 ".*".

  • Create the 'helloWorld' record in the publisher context (the record will be referenced by the context until it is destroyed)
            publisherContext.getLocalContainer("helloWorld", Type.RECORD, Domain.get(1, "Tutorial"))
    
  • Add a statement to pause the main thread to accept input (Fulmine does not create any non-daemon threads so the tutorial program would terminate without this)
            try
            {
                System.in.read();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
    
  • Run the tutorial program. You should see the following in the console for subscriberContext (allow 10 seconds for the contexts to discover each other and send subscription data)
    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.