You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@zookeeper.apache.org by Mohan <ra...@gmail.com> on 2012/02/23 09:31:40 UTC

zookeeper / proto buffer example

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.