Package org.fusesource.hawtdispatch

Examples of org.fusesource.hawtdispatch.Task


                                    case CONNECTION_ACCEPTED:
                                        mqtt.tracer.debug("MQTT login accepted");
                                        onSessionEstablished(transport);
                                        cb.onSuccess(null);
                                        listener.onConnected();
                                        queue.execute(new Task() {
                                            @Override
                                            public void run() {
                                                drainOverflow();
                                            }
                                        });
View Full Code Here


        // Handle a Ctrl-C event cleanly.
        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run() {
                setName("MQTT client shutdown");
                connection.getDispatchQueue().execute(new Task() {
                    public void run() {
                        connection.disconnect(new Callback<Void>() {
                            public void onSuccess(Void value) {
                                done.countDown();
                            }

                            public void onFailure(Throwable value) {
                                done.countDown();
                            }
                        });
                    }
                });
            }
        });
       
        connection.listener(new org.fusesource.mqtt.client.Listener() {

            public void onConnected() {
                if (debug) {
                    stderr("Connected");
                }
            }

            public void onDisconnected() {
                if (debug) {
                    stderr("Disconnected");
                }
            }

            public void onPublish(UTF8Buffer topic, Buffer body, Runnable ack) {
            }

            public void onFailure(Throwable value) {
                if (debug) {
                    value.printStackTrace();
                } else {
                    stderr(value);
                }
                System.exit(2);
            }
        });

        connection.resume();
        connection.connect(new Callback<Void>() {
            public void onFailure(Throwable value) {
                if (debug) {
                    value.printStackTrace();
                } else {
                    stderr(value);
                }
                System.exit(2);
            }
            public void onSuccess(Void value) {
            }
        });

        new Task() {
            long sent = 0;
            public void run() {
                final Task publish = this;
                Buffer message  = body;
                if(prefixCounter) {
                    long id = sent + 1;
                    ByteArrayOutputStream os = new ByteArrayOutputStream(message.length + 15);
                    os.write(new AsciiBuffer(Long.toString(id)));
View Full Code Here

            public void run() {
                setName("MQTT client shutdown");
                if(debug) {
                    stderr("Disconnecting the client.");
                }
                connection.getDispatchQueue().execute(new Task() {
                    public void run() {
                        connection.disconnect(new Callback<Void>() {
                            public void onSuccess(Void value) {
                                done.countDown();
                            }
View Full Code Here

    }


    public Future<Void> connect() {
        final Promise<Void> future = new Promise<Void>();
        next.getDispatchQueue().execute(new Task() {
            public void run() {
                next.connect(future);
            }
        });
        return future;
View Full Code Here

        return future;
    }

    public Future<Void> disconnect() {
        final Promise<Void> future = new Promise<Void>();
        next.getDispatchQueue().execute(new Task() {
            public void run() {
                next.disconnect(future);
            }
        });
        return future;
View Full Code Here

        return future;
    }

    public Future<Void> kill() {
        final Promise<Void> future = new Promise<Void>();
        next.getDispatchQueue().execute(new Task() {
            public void run() {
                next.kill(future);
            }
        });
        return future;
View Full Code Here

        return future;
    }

    public Future<byte[]> subscribe(final Topic[] topics) {
        final Promise<byte[]> future = new Promise<byte[]>();
        next.getDispatchQueue().execute(new Task() {
            public void run() {
                next.subscribe(topics, future);
            }
        });
        return future;
View Full Code Here

        return unsubscribe(buffers);
    }

    public Future<Void> unsubscribe(final UTF8Buffer[] topics) {
        final Promise<Void> future = new Promise<Void>();
        next.getDispatchQueue().execute(new Task() {
            public void run() {
                next.unsubscribe(topics, future);
            }
        });
        return future;
View Full Code Here

    } catch (URISyntaxException e) {
      e.printStackTrace();
    }
    final AmqpConnection conn = AmqpConnection.connect(options );
    _logger.fine("connection queue");
    conn.queue().execute(new Task() {

      @Override
      public void run() {
        _logger.fine("connection running, setup callbacks");
        conn.onTransportFailure(new Callback<Throwable>() {

          @Override
          public void onSuccess(Throwable value) {
            _logger.fine("transportFailure Success? " + str(value));
            conn.close();
          }

          @Override
          public void onFailure(Throwable value) {
            _logger.fine("transportFailure Trouble! " + str(value));
            conn.close();
          }
        });

        conn.onConnected(new Callback<Void>() {

          @Override
          public void onSuccess(Void value) {
            _logger.fine("on connect Success! in container " + container);
            final AmqpSession session = conn.createSession();
            Target rqtarget = new Target();
            rqtarget.setAddress("rq-tgt");
            final AmqpSender sender = session.createSender(rqtarget, QoS.AT_LEAST_ONCE, "request-yyy");
            Source rqsource = new Source();
            rqsource.setAddress("rs-src");
            sender.getEndpoint().setSource(rqsource);
            Source rssource = new Source();
            rssource.setAddress("rs-src");
            final AmqpReceiver receiver = session.createReceiver(rssource , QoS.AT_LEAST_ONCE, 10, "response-yyy");
            Target rstarget = new Target();
            final String address = "rs-tgt";
            rstarget.setAddress(address);
            receiver.getEndpoint().setTarget(rstarget);
            sender.onRemoteClose(new Callback<ErrorCondition>() {

              @Override
              public void onSuccess(ErrorCondition value) {
                _logger.fine("sender remote close!" + str(value));
              }

              @Override
              public void onFailure(Throwable value) {
                _logger.fine("sender remote close Trouble!" + str(value));
                conn.close();

              }

            });
            receiver.onRemoteClose(new Callback<ErrorCondition>() {

              @Override
              public void onSuccess(ErrorCondition value) {
                _logger.fine("receiver remote close!" + str(value));
              }

              @Override
              public void onFailure(Throwable value) {
                _logger.fine("receiver remote close Trouble!" + str(value));
                conn.close();

              }

            });

            final Task work = new Task() {

              private AtomicInteger count = new AtomicInteger();

              @Override
              public void run() {
                Message message = session.createTextMessage("hello world! " + String.valueOf(count.incrementAndGet()));
                message.setAddress("amqp://joze/rq-src");
                String reply_to = "amqp://" + container + "/" + address;
                message.setReplyTo(reply_to);
                message.setCorrelationId("correlator");
                final MessageDelivery md = sender.send(message);
                md.onRemoteStateChange(new Callback<DeliveryState>() {

                  @Override
                  public void onSuccess(DeliveryState value) {
                    _logger.fine("delivery remote state change! " + str(value) +
                        " local: "+ str(md.getLocalState()) +
                        " remote: " + str(md.getRemoteState()));
                  }

                  @Override
                  public void onFailure(Throwable value) {
                    _logger.fine("remote state change Trouble!" + str(value));
                    conn.close();
                  }

                });
                md.onSettle(new Callback<DeliveryState>() {

                  @Override
                  public void onSuccess(DeliveryState value) {
                    _logger.fine("delivery settled! " + str(value) +
                        " local: "+ str(md.getLocalState()) +
                        " remote: " + str(md.getRemoteState()));
                    _logger.fine("sender settle mode state " +
                        " local receiver " + str(sender.getEndpoint().getReceiverSettleMode()) +
                        " local sender " + str(sender.getEndpoint().getSenderSettleMode()) +
                        " remote receiver " + str(sender.getEndpoint().getRemoteReceiverSettleMode()) +
                        " remote sender " + str(sender.getEndpoint().getRemoteSenderSettleMode()) +
                        ""
                        );
                  }

                  @Override
                  public void onFailure(Throwable value) {
                    _logger.fine("delivery sending Trouble!" + str(value));
                    conn.close();
                  }
                });
              }

            };
            receiver.setDeliveryListener(new AmqpDeliveryListener() {

              @Override
              public void onMessageDelivery(
                  MessageDelivery delivery) {
                Message message = delivery.getMessage();
                _logger.fine("incoming message delivery! " +
                    " local " + str(delivery.getLocalState()) +
                    " remote " + str(delivery.getRemoteState()) +
                    " message " + str(message.getBody()) +
                    "");
                delivery.onSettle(new Callback<DeliveryState>() {

                  @Override
                  public void onSuccess(DeliveryState value) {
                    _logger.fine("incoming message settled! ");
                    int i = countdown.decrementAndGet();
                    if ( i > 0 ) {
                      _logger.fine("More work " + str(i));
                      work.run();
                    } else {
                      conn.queue().executeAfter(100, TimeUnit.MILLISECONDS, new Task() {

                        @Override
                        public void run() {
                          _logger.fine("stopping sender");
                          sender.close();                       
                        }
                      });
                      conn.queue().executeAfter(200, TimeUnit.MILLISECONDS, new Task() {

                        @Override
                        public void run() {
                          _logger.fine("stopping receiver");
                          receiver.close();

                        }
                      });
                      conn.queue().executeAfter(300, TimeUnit.MILLISECONDS, new Task() {

                        @Override
                        public void run() {
                          _logger.fine("stopping session");
                          session.close();

                        }
                      });
                      conn.queue().executeAfter(400, TimeUnit.MILLISECONDS, new Task() {

                        @Override
                        public void run() {
                          _logger.fine("stopping connection");
                          conn.close();
View Full Code Here

    /**
     * @return the future remote delivery state when it changes.
     */
    public Future<DeliveryState> getRemoteStateChangeFuture() {
        final Promise<DeliveryState> rc = new Promise<DeliveryState>();
        link().queue().execute(new Task() {
            @Override
            public void run() {
                onRemoteStateChange(rc);
            }
        });
View Full Code Here

TOP

Related Classes of org.fusesource.hawtdispatch.Task

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.