11 changed files with 170 additions and 22 deletions
@ -0,0 +1,8 @@ |
|||||
|
package org.thingsboard.server; |
||||
|
|
||||
|
public interface TbQueueCallback { |
||||
|
|
||||
|
void onSuccess(); |
||||
|
|
||||
|
void onFailure(Throwable t); |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
package org.thingsboard.server; |
||||
|
|
||||
|
import java.util.List; |
||||
|
|
||||
|
public interface TbQueueConsumer<T extends TbQueueMsg> { |
||||
|
|
||||
|
List<TbQueueMsg> poll(); |
||||
|
|
||||
|
void commit(); |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
package org.thingsboard.server; |
||||
|
|
||||
|
import java.util.Map; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
public interface TbQueueMsg { |
||||
|
|
||||
|
UUID getKey(); |
||||
|
|
||||
|
Map<String, byte[]> getHeaders(); |
||||
|
|
||||
|
byte[] getData(); |
||||
|
} |
||||
@ -0,0 +1,4 @@ |
|||||
|
package org.thingsboard.server; |
||||
|
|
||||
|
public interface TbQueueMsgMetadata { |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
package org.thingsboard.server; |
||||
|
|
||||
|
import com.google.common.util.concurrent.ListenableFuture; |
||||
|
|
||||
|
public interface TbQueueProducer<T extends TbQueueMsg> { |
||||
|
|
||||
|
ListenableFuture<TbQueueMsgMetadata> send(T msg, TbQueueCallback callback); |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
package org.thingsboard.server; |
||||
|
|
||||
|
import com.google.common.util.concurrent.ListenableFuture; |
||||
|
|
||||
|
public interface TbQueueRequestTemplate<Request extends TbQueueMsg, Response extends TbQueueMsg> { |
||||
|
|
||||
|
ListenableFuture<Response> post(String key, Request request); |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
package org.thingsboard.server.common.transport.queue; |
||||
|
|
||||
|
import java.nio.ByteBuffer; |
||||
|
import java.nio.charset.StandardCharsets; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
public abstract class TransportApiCall { |
||||
|
|
||||
|
protected byte[] uuidToBytes(UUID uuid) { |
||||
|
ByteBuffer buf = ByteBuffer.allocate(16); |
||||
|
buf.putLong(uuid.getMostSignificantBits()); |
||||
|
buf.putLong(uuid.getLeastSignificantBits()); |
||||
|
return buf.array(); |
||||
|
} |
||||
|
|
||||
|
protected static UUID bytesToUuid(byte[] bytes) { |
||||
|
ByteBuffer bb = ByteBuffer.wrap(bytes); |
||||
|
long firstLong = bb.getLong(); |
||||
|
long secondLong = bb.getLong(); |
||||
|
return new UUID(firstLong, secondLong); |
||||
|
} |
||||
|
|
||||
|
protected byte[] stringToBytes(String string) { |
||||
|
return string.getBytes(StandardCharsets.UTF_8); |
||||
|
} |
||||
|
|
||||
|
protected String bytesToString(byte[] data) { |
||||
|
return new String(data, StandardCharsets.UTF_8); |
||||
|
} |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,40 @@ |
|||||
|
package org.thingsboard.server.common.transport.queue; |
||||
|
|
||||
|
import org.thingsboard.server.TbQueueMsg; |
||||
|
import org.thingsboard.server.gen.transport.TransportProtos; |
||||
|
|
||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
public class TransportApiCallRequest extends TransportApiCall implements TbQueueMsg { |
||||
|
public static final String REQUEST_ID_HEADER = "requestId"; |
||||
|
public static final String RESPONSE_TOPIC_HEADER = "responseTopic"; |
||||
|
|
||||
|
private final UUID requestId; |
||||
|
private final Map<String, byte[]> headers; |
||||
|
private final TransportProtos.TransportApiRequestMsg msg; |
||||
|
|
||||
|
public TransportApiCallRequest(UUID requestId, String responseTopic, TransportProtos.TransportApiRequestMsg msg) { |
||||
|
this.requestId = requestId; |
||||
|
this.headers = new HashMap<>(); |
||||
|
this.headers.put(REQUEST_ID_HEADER, uuidToBytes(requestId)); |
||||
|
this.headers.put(RESPONSE_TOPIC_HEADER, stringToBytes(responseTopic)); |
||||
|
this.msg = msg; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public UUID getKey() { |
||||
|
return requestId; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Map<String, byte[]> getHeaders() { |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public byte[] getData() { |
||||
|
return msg.toByteArray(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,19 @@ |
|||||
|
package org.thingsboard.server.common.transport.queue; |
||||
|
|
||||
|
import org.thingsboard.server.TbQueueConsumer; |
||||
|
import org.thingsboard.server.TbQueueMsg; |
||||
|
import org.thingsboard.server.TbQueueProducer; |
||||
|
|
||||
|
public interface TransportQueueProvider { |
||||
|
|
||||
|
TbQueueProducer<TransportApiCallRequest> getTransportApiCallRequestsProducer(); |
||||
|
|
||||
|
TbQueueConsumer<TbQueueMsg> getTransportApiCallResponsesConsumer(); |
||||
|
|
||||
|
TbQueueProducer<TbQueueMsg> getRuleEngineMsgProducer(); |
||||
|
|
||||
|
TbQueueProducer<TbQueueMsg> getTbCoreMsgProducer(); |
||||
|
|
||||
|
TbQueueConsumer<TbQueueMsg> getTransportNotificationsConsumer(); |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue