[jgroups-dev] Programmatic API for stack creation

Bela Ban <[email protected]>
Newsgroups gmane.comp.java.javagroups.devel,gmane.comp.java.javagroups.general
Message-ID <[email protected]>
I've checked in a first version of a programmatic API [1] to create a 
stack. This allows developers to create a channel and protocol stack 
without the use of an XML configuration file.


The example below shows how to do it; it is part of the JGroups src: 
org.jgroups.demos.ProgrammaticChat.

First, a JChannel is created with false as arg, which means that no 
ProtocolStack should be created.

Then, we create a ProtocolStack with an empty protocol configuration, so 
no protocols will be created off of an XML config file.

Then we create the individual protocols and add them to the 
ProtocolStack. Note that Protocol.setValue(String name, Object val) and 
Protocol.setValues(Map<String,Object> values) has been added, and allows 
for quick setting of protocol fields via reflection.

Then, we need to call init() on the freshly created stack. This sets 
default values and calls init() on every protocol. (Note that shared 
transport support has not yet been added/tested).

The rest of the code is the same as if you'd create a JChannel via an 
XML config.


public class ProgrammaticChat {

    public static void main(String[] args) throws Exception {
        JChannel ch=new JChannel(false);
        ProtocolStack stack=ch.createProtocolStack();
        stack.addProtocol(new UDP().setValue("bind_addr", 
InetAddress.getByName("192.168.1.5"))).
                addProtocol(new PING()).addProtocol(new 
MERGE2()).addProtocol(new FD_SOCK()).
                addProtocol(new FD_ALL()).addProtocol(new 
VERIFY_SUSPECT()).addProtocol(new BARRIER()).
                addProtocol(new NAKACK()).addProtocol(new 
UNICAST2()).addProtocol(new STABLE()).
                addProtocol(new GMS()).addProtocol(new 
UFC()).addProtocol(new MFC()).addProtocol(new FRAG2());
        stack.init();

        ch.setReceiver(new ReceiverAdapter() {
            public void viewAccepted(View new_view) {
                System.out.println("view: " + new_view);
            }

            public void receive(Message msg) {
                System.out.println("<< " + msg.getObject() + " [" + 
msg.getSrc() + "]");
            }
        });

        ch.connect("ChatCluster");


        for(;;) {
            String line=Util.readStringFromStdin(": ");
            ch.send(null, null, line);
        }
    }

}



Let me know what you think of this; feedback is essential at this point 
! What's missing, what should be added, what should be removed and what 
should be changed ?
Cheers,


[1] https://jira.jboss.org/browse/JGRP-1245

-- 
Bela Ban
Lead JGroups / Clustering Team
JBoss


------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1,  ECMAScript5, and DOM L2 & L3.
Spend less time writing and  rewriting code and more time creating great
experiences on the web. Be a part of the beta today.
http://p.sf.net/sfu/beautyoftheweb
_______________________________________________
Javagroups-development mailing list
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.