org.fusesource.stomp.client.FutureConnection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of stompjms-client Show documentation
Show all versions of stompjms-client Show documentation
STOMP-JMS is a JMS implementation using STOMP as the wire protocol
/**
* Copyright (C) 2010-2011, FuseSource Corp. All rights reserved.
*
* http://fusesource.com
*
* The software in this package is published under the terms of the
* CDDL license a copy of which has been included with this distribution
* in the license.txt file.
*/
package org.fusesource.stomp.client;
import org.fusesource.hawtbuf.AsciiBuffer;
import org.fusesource.hawtdispatch.DispatchQueue;
import org.fusesource.hawtdispatch.Task;
import org.fusesource.stomp.codec.StompFrame;
import java.util.ArrayList;
import java.util.LinkedList;
/**
*
*
*
* @author Hiram Chirino
*/
public class FutureConnection {
private final CallbackConnection connection;
private LinkedList> receiveFutures = new LinkedList>();
private LinkedList receivedFrames = new LinkedList();
FutureConnection(CallbackConnection connection) {
this.connection = connection;
this.connection.receive(new Callback() {
@Override
public void onFailure(Throwable value) {
getDispatchQueue().assertExecuting();
ArrayList> tmp = new ArrayList>(receiveFutures);
receiveFutures.clear();
for (Promise future : tmp) {
future.onFailure(value);
}
}
@Override
public void onSuccess(StompFrame value) {
getDispatchQueue().assertExecuting();
if( receiveFutures.isEmpty() ) {
receivedFrames.add(value);
} else {
receiveFutures.removeFirst().onSuccess(value);
}
}
});
this.connection.resume();
}
public StompFrame connectedFrame() {
return connection.connectedFrame();
}
private DispatchQueue getDispatchQueue() {
return this.connection.getDispatchQueue();
}
public Future close() {
final Promise future = new Promise();
connection.close(new Runnable() {
public void run() {
future.onSuccess(null);
}
});
return future;
}
public AsciiBuffer nextId() {
return connection.nextId();
}
public AsciiBuffer nextId(String prefix) {
return connection.nextId(prefix);
}
public Future request(final StompFrame frame) {
final Promise future = new Promise();
connection.getDispatchQueue().execute(new Task() {
public void run() {
connection.request(frame, future);
}
});
return future;
}
public Future send(final StompFrame frame) {
final Promise future = new Promise();
connection.getDispatchQueue().execute(new Task() {
public void run() {
connection.send(frame, future);
}
});
return future;
}
public Future receive() {
final Promise future = new Promise();
getDispatchQueue().execute(new Task(){
public void run() {
if( connection.getFailure()!=null ) {
future.onFailure(connection.getFailure());
} else {
if( receivedFrames.isEmpty() ) {
receiveFutures.add(future);
} else {
future.onSuccess(receivedFrames.removeFirst());
}
}
}
});
return future;
}
public void resume() {
connection.resume();
}
public void suspend() {
connection.suspend();
}
}