zookeeper-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mohan <radhakrishnan.mo...@gmail.com>
Subject zookeeper / proto buffer example
Date Thu, 23 Feb 2012 08:31:40 GMT
I have a simple zookeeper client that tries to use proto buffer for
serialization. It works partly.

Producer - Seems to work

    public void send() throws IOException, InterruptedException,
KeeperException {

        if( null != zkc ){
            ZooKeeper zk = zkc.getZookeeper();
            zk.create( root + "/protocolbuffer",
                       getData(),
                       ZooDefs.Ids.OPEN_ACL_UNSAFE,
                       CreateMode.PERSISTENT_SEQUENTIAL
                      );
        }
    }

    private byte[] getData() throws InvalidProtocolBufferException {
        Message.Load message = Message.Load.newBuilder().setType(
                                 ( Message.Load.LoadType.HIGH)).build();
        Message.Load message1 = Message.Load.parseFrom(
message.toByteArray() );
        System.out.println( "Distributed value is [" +
message1.getType().toString() +"]");
        return message.toByteArray();
    }

Consumer - throws InvalidProtocolBufferException( Invalid tag '0' )

    public void receive() throws IOException, InterruptedException,
KeeperException {

        Stat stat = null;

            if( null != zkc ){
                ZooKeeper zk = zkc.getZookeeper();
                if( null != zk ){
                    byte[] value =
                    zk.getData( root + "/protocolbuffer",
                               false,
                               stat
                              );
                    System.out.println( "Stat is  [" + stat + "]");
                    getData( value );
                }
            }
    }

    private void getData( byte[] value ){

        try {
            System.out.println( "Distributed value is  [" + value + "]");
            Message.Load message = Message.Load.parseFrom( value );

        } catch (InvalidProtocolBufferException e) {

            System.out.println( "InvalidProtocolBufferException" +
e.getMessage() );
            e.printStackTrace();

        }
    }


Are there any such simple examples to refer to ?

Thanks.

--
View this message in context: http://zookeeper-user.578899.n2.nabble.com/zookeeper-proto-buffer-example-tp7311186p7311186.html
Sent from the zookeeper-user mailing list archive at Nabble.com.

Mime
View raw message