use of com.rabbitmq.stream.StreamException in project rabbitmq-stream-java-client by rabbitmq.
the class Client method partitions.
public List<String> partitions(String superStream) {
if (superStream == null) {
throw new IllegalArgumentException("stream must not be null");
}
int length = // API code, version, correlation ID, 1 string
2 + 2 + 4 + +2 + superStream.length();
int correlationId = correlationSequence.incrementAndGet();
try {
ByteBuf bb = allocate(length + 4);
bb.writeInt(length);
bb.writeShort(encodeRequestCode(COMMAND_PARTITIONS));
bb.writeShort(VERSION_1);
bb.writeInt(correlationId);
bb.writeShort(superStream.length());
bb.writeBytes(superStream.getBytes(StandardCharsets.UTF_8));
OutstandingRequest<List<String>> request = new OutstandingRequest<>(this.rpcTimeout);
outstandingRequests.put(correlationId, request);
channel.writeAndFlush(bb);
request.block();
return request.response.get();
} catch (RuntimeException e) {
outstandingRequests.remove(correlationId);
throw new StreamException(e);
}
}
use of com.rabbitmq.stream.StreamException in project rabbitmq-stream-java-client by rabbitmq.
the class Client method authenticate.
private void authenticate() {
List<String> saslMechanisms = getSaslMechanisms();
SaslMechanism saslMechanism = this.saslConfiguration.getSaslMechanism(saslMechanisms);
byte[] challenge = null;
boolean authDone = false;
while (!authDone) {
byte[] saslResponse = saslMechanism.handleChallenge(challenge, this.credentialsProvider);
SaslAuthenticateResponse saslAuthenticateResponse = sendSaslAuthenticate(saslMechanism, saslResponse);
if (saslAuthenticateResponse.isOk()) {
authDone = true;
} else if (saslAuthenticateResponse.isChallenge()) {
challenge = saslAuthenticateResponse.challenge;
} else if (saslAuthenticateResponse.isAuthenticationFailure()) {
throw new AuthenticationFailureException("Unexpected response code during authentication: " + formatConstant(saslAuthenticateResponse.getResponseCode()));
} else {
throw new StreamException("Unexpected response code during authentication: " + formatConstant(saslAuthenticateResponse.getResponseCode()));
}
}
}
use of com.rabbitmq.stream.StreamException in project rabbitmq-stream-java-client by rabbitmq.
the class Client method subscribe.
/**
* Subscribe to receive messages from a stream.
*
* <p>Note the offset is an unsigned long. Longs are signed in Java, but unsigned longs can be
* used as long as some care is taken for some operations. See the <code>unsigned*</code> static
* methods in {@link Long}.
*
* @param subscriptionId identifier to correlate inbound messages to this subscription
* @param stream the stream to consume from
* @param offsetSpecification the specification of the offset to consume from
* @param credit the initial number of credits
* @param properties some optional properties to describe the subscription
* @return the subscription confirmation
*/
public Response subscribe(byte subscriptionId, String stream, OffsetSpecification offsetSpecification, int credit, Map<String, String> properties) {
if (credit < 0 || credit > Short.MAX_VALUE) {
throw new IllegalArgumentException("Credit value must be between 0 and " + Short.MAX_VALUE);
}
// misses the offset
int length = 2 + 2 + 4 + 1 + 2 + stream.length() + 2 + 2;
if (offsetSpecification.isOffset() || offsetSpecification.isTimestamp()) {
length += 8;
}
int propertiesSize = 0;
if (properties != null && !properties.isEmpty()) {
// size of the map
propertiesSize = 4;
for (Map.Entry<String, String> entry : properties.entrySet()) {
propertiesSize += 2 + entry.getKey().length() + 2 + entry.getValue().length();
}
}
length += propertiesSize;
int correlationId = correlationSequence.getAndIncrement();
try {
ByteBuf bb = allocate(length + 4);
bb.writeInt(length);
bb.writeShort(encodeRequestCode(COMMAND_SUBSCRIBE));
bb.writeShort(VERSION_1);
bb.writeInt(correlationId);
bb.writeByte(subscriptionId);
bb.writeShort(stream.length());
bb.writeBytes(stream.getBytes(StandardCharsets.UTF_8));
bb.writeShort(offsetSpecification.getType());
if (offsetSpecification.isOffset() || offsetSpecification.isTimestamp()) {
bb.writeLong(offsetSpecification.getOffset());
}
bb.writeShort(credit);
if (properties != null && !properties.isEmpty()) {
bb.writeInt(properties.size());
for (Map.Entry<String, String> entry : properties.entrySet()) {
bb.writeShort(entry.getKey().length()).writeBytes(entry.getKey().getBytes(StandardCharsets.UTF_8)).writeShort(entry.getValue().length()).writeBytes(entry.getValue().getBytes(StandardCharsets.UTF_8));
}
}
OutstandingRequest<Response> request = new OutstandingRequest<>(this.rpcTimeout);
outstandingRequests.put(correlationId, request);
if (offsetSpecification.isOffset()) {
subscriptionOffsets.add(new SubscriptionOffset(subscriptionId, offsetSpecification.getOffset()));
}
channel.writeAndFlush(bb);
request.block();
return request.response.get();
} catch (RuntimeException e) {
outstandingRequests.remove(correlationId);
throw new StreamException(e);
}
}
use of com.rabbitmq.stream.StreamException in project rabbitmq-stream-java-client by rabbitmq.
the class Client method unsubscribe.
public Response unsubscribe(byte subscriptionId) {
int length = 2 + 2 + 4 + 1;
int correlationId = correlationSequence.getAndIncrement();
try {
ByteBuf bb = allocate(length + 4);
bb.writeInt(length);
bb.writeShort(encodeRequestCode(COMMAND_UNSUBSCRIBE));
bb.writeShort(VERSION_1);
bb.writeInt(correlationId);
bb.writeByte(subscriptionId);
OutstandingRequest<Response> request = new OutstandingRequest<>(this.rpcTimeout);
outstandingRequests.put(correlationId, request);
channel.writeAndFlush(bb);
request.block();
return request.response.get();
} catch (RuntimeException e) {
outstandingRequests.remove(correlationId);
throw new StreamException(e);
}
}
use of com.rabbitmq.stream.StreamException in project rabbitmq-stream-java-client by rabbitmq.
the class Client method send.
// for testing
void send(byte[] content) {
ByteBuf bb = allocateNoCheck(content.length);
bb.writeBytes(content);
try {
channel.writeAndFlush(bb).sync();
} catch (InterruptedException e) {
throw new StreamException(e);
}
}
Aggregations