Search in sources :

Example 1 with Callback

use of org.fusesource.stomp.client.Callback in project camel by apache.

the class StompEndpoint method doStart.

@Override
protected void doStart() throws Exception {
    final Promise<CallbackConnection> promise = new Promise<>();
    stomp = new Stomp(configuration.getBrokerURL());
    stomp.setLogin(configuration.getLogin());
    stomp.setPasscode(configuration.getPasscode());
    if (configuration.getSslContextParameters() != null) {
        stomp.setSslContext(configuration.getSslContextParameters().createSSLContext(getCamelContext()));
    }
    stomp.connectCallback(promise);
    if (configuration.getHost() != null && !configuration.getHost().isEmpty()) {
        stomp.setHost(configuration.getHost());
    }
    connection = promise.await();
    connection.getDispatchQueue().execute(new Task() {

        @Override
        public void run() {
            connection.receive(new Callback<StompFrame>() {

                @Override
                public void onFailure(Throwable value) {
                    if (started.get()) {
                        connection.close(null);
                    }
                }

                @Override
                public void onSuccess(StompFrame value) {
                    if (!consumers.isEmpty()) {
                        Exchange exchange = createExchange();
                        exchange.getIn().setBody(value.content());
                        for (StompConsumer consumer : consumers) {
                            consumer.processExchange(exchange);
                        }
                    }
                }
            });
            connection.resume();
        }
    });
}
Also used : Exchange(org.apache.camel.Exchange) Promise(org.fusesource.stomp.client.Promise) CallbackConnection(org.fusesource.stomp.client.CallbackConnection) Task(org.fusesource.hawtdispatch.Task) Callback(org.fusesource.stomp.client.Callback) AsyncCallback(org.apache.camel.AsyncCallback) StompFrame(org.fusesource.stomp.codec.StompFrame) Stomp(org.fusesource.stomp.client.Stomp)

Example 2 with Callback

use of org.fusesource.stomp.client.Callback in project camel by apache.

the class StompEndpoint method send.

protected void send(final Exchange exchange, final AsyncCallback callback) {
    final StompFrame frame = new StompFrame(SEND);
    frame.addHeader(DESTINATION, StompFrame.encodeHeader(destination));
    //Fix for CAMEL-9506 leveraging the camel converter to do the change
    frame.content(utf8(exchange.getIn().getBody(String.class)));
    connection.getDispatchQueue().execute(new Task() {

        @Override
        public void run() {
            connection.send(frame, new Callback<Void>() {

                @Override
                public void onFailure(Throwable e) {
                    exchange.setException(e);
                    callback.done(false);
                }

                @Override
                public void onSuccess(Void v) {
                    callback.done(false);
                }
            });
        }
    });
}
Also used : Task(org.fusesource.hawtdispatch.Task) Callback(org.fusesource.stomp.client.Callback) AsyncCallback(org.apache.camel.AsyncCallback) StompFrame(org.fusesource.stomp.codec.StompFrame)

Aggregations

AsyncCallback (org.apache.camel.AsyncCallback)2 Task (org.fusesource.hawtdispatch.Task)2 Callback (org.fusesource.stomp.client.Callback)2 StompFrame (org.fusesource.stomp.codec.StompFrame)2 Exchange (org.apache.camel.Exchange)1 CallbackConnection (org.fusesource.stomp.client.CallbackConnection)1 Promise (org.fusesource.stomp.client.Promise)1 Stomp (org.fusesource.stomp.client.Stomp)1