Connect

Connecting synchronously

        BrokerClient bk = new BrokerClient();

        bk.addServer("broker.bk.sapo.pt",3323);

        bk.connect(); // if connection is not possible a runtime exception will be thrown 

Connecting asynchronously

        BrokerClient bk = new BrokerClient();

        bk.addServer("broker.bk.sapo.pt",3323);

        Future<HostInfo> f = bk.connectAsync();


        f.get(); if connection is not possible a runtime exception will be thrown 

Subscribe

subscribe a queue or topic

        BrokerClient bk = new BrokerClient();

        // ... connecting ...

       Future<HostInfo> f = bk.subscribe("/teste/",NetAction.DestinationType.QUEUE,new NotificationListenerAdapter() {

                   @Override
                   public boolean onMessage(NetNotification message, HostInfo host) {

                       // do something

                       return true; // return true or false to acknowledge or not 
                   }

       });


       f.get(); //wait for subscription message to be sent

subscribe a queue using polling

        BrokerClient bk = new BrokerClient();

        // ... connecting ...

        while (true){


                  NetNotification notification = bk.poll("/teste/"); //blocks!!!

                  // ... do some work ... 

                  bk.acknowledge(notification); //acknowledge the message


                  if( ... ){ // step out on some condition
                        break;
                  }

        }

subscribe a queue using polling with timeout

        BrokerClient bk = new BrokerClient();

        // ... connecting ...

        long timeout = 5000;

        while (true){

                   try{

                        NetNotification notification = bk.poll("/teste/",timeout); //blocks!!!

                        // ... do some work ... 

                        bk.acknowledge(notification); //acknowledge the message


                   }catch (TimeoutException e){

                        // there was a timeout

                    }


                    if( ... ){ // step out on some condition
                        break;
                    }

        }

Publish

publish a message

        BrokerClient bk = new BrokerClient();

        // ... connecting ...

       NetAction.DestinationType dstType = NetAction.DestinationType.QUEUE; // or TOPIC 

       Future<HostInfo> future = bk.publish("Olá Mundo", "/teste/", dstType);

Publishing messages via UDP

       UdpBrokerClient bk = new UdpBrokerClient();

       bk.addServer("broker.bk.sapo.pt",3323); 


       NetAction.DestinationType dstType = NetAction.DestinationType.QUEUE; // or TOPIC 

       Future<HostInfo> future = bk.publish("Olá Mundo", "/teste/", dstType);