use of com.swiftmq.amqp.v100.generated.messaging.message_format.Properties in project rabbitmq-stream-java-client by rabbitmq.
the class SwiftMqCodec method encode.
@Override
public EncodedMessage encode(Message message) {
AMQPMessage outboundMessage;
if (message instanceof SwiftMqAmqpMessageWrapper) {
outboundMessage = ((SwiftMqAmqpMessageWrapper) message).message;
} else {
outboundMessage = new AMQPMessage();
if (message.getProperties() != null) {
com.rabbitmq.stream.Properties headers = message.getProperties();
com.swiftmq.amqp.v100.generated.messaging.message_format.Properties properties = new com.swiftmq.amqp.v100.generated.messaging.message_format.Properties();
boolean propertiesSet = false;
if (headers.getMessageId() != null) {
if (headers.getMessageId() instanceof String) {
properties.setMessageId(new MessageIdString(headers.getMessageIdAsString()));
} else if (headers.getMessageId().getClass().isPrimitive() || headers.getMessageId() instanceof Long) {
properties.setMessageId(new MessageIdUlong(headers.getMessageIdAsLong()));
} else if (headers.getMessageId().getClass().isArray()) {
properties.setMessageId(new MessageIdBinary(headers.getMessageIdAsBinary()));
} else if (headers.getMessageId() instanceof UUID) {
properties.setMessageId(new MessageIdUuid(headers.getMessageIdAsUuid()));
} else {
throw new IllegalStateException("Type not supported for message ID:" + properties.getMessageId().getClass());
}
propertiesSet = true;
}
if (headers.getUserId() != null) {
properties.setUserId(new AMQPBinary(headers.getUserId()));
propertiesSet = true;
}
if (headers.getTo() != null) {
properties.setTo(new AddressString(headers.getTo()));
propertiesSet = true;
}
if (headers.getSubject() != null) {
properties.setSubject(new AMQPString(headers.getSubject()));
propertiesSet = true;
}
if (headers.getReplyTo() != null) {
properties.setReplyTo(new AddressString(headers.getReplyTo()));
propertiesSet = true;
}
if (headers.getCorrelationId() != null) {
if (headers.getCorrelationId() instanceof String) {
properties.setCorrelationId(new MessageIdString(headers.getCorrelationIdAsString()));
} else if (headers.getCorrelationId().getClass().isPrimitive() || headers.getCorrelationId() instanceof Long) {
properties.setCorrelationId(new MessageIdUlong(headers.getCorrelationIdAsLong()));
} else if (headers.getCorrelationId().getClass().isArray()) {
properties.setCorrelationId(new MessageIdBinary(headers.getCorrelationIdAsBinary()));
} else if (headers.getCorrelationId() instanceof UUID) {
properties.setCorrelationId(new MessageIdUuid(headers.getCorrelationIdAsUuid()));
} else {
throw new IllegalStateException("Type not supported for correlation ID:" + properties.getCorrelationId().getClass());
}
propertiesSet = true;
}
if (headers.getContentType() != null) {
properties.setContentType(new AMQPSymbol(headers.getContentType()));
propertiesSet = true;
}
if (headers.getContentEncoding() != null) {
properties.setContentEncoding(new AMQPSymbol(headers.getContentEncoding()));
propertiesSet = true;
}
if (headers.getAbsoluteExpiryTime() > 0) {
properties.setAbsoluteExpiryTime(new AMQPTimestamp(headers.getAbsoluteExpiryTime()));
propertiesSet = true;
}
if (headers.getCreationTime() > 0) {
properties.setCreationTime(new AMQPTimestamp(headers.getCreationTime()));
propertiesSet = true;
}
if (headers.getGroupId() != null) {
properties.setGroupId(new AMQPString(headers.getGroupId()));
propertiesSet = true;
}
if (headers.getGroupSequence() >= 0) {
properties.setGroupSequence(new SequenceNo(headers.getGroupSequence()));
propertiesSet = true;
}
if (headers.getReplyToGroupId() != null) {
properties.setReplyToGroupId(new AMQPString(headers.getReplyToGroupId()));
propertiesSet = true;
}
if (propertiesSet) {
outboundMessage.setProperties(properties);
}
}
if (message.getApplicationProperties() != null && !message.getApplicationProperties().isEmpty()) {
Map<AMQPType, AMQPType> applicationProperties = new LinkedHashMap<>(message.getApplicationProperties().size());
for (Map.Entry<String, Object> entry : message.getApplicationProperties().entrySet()) {
applicationProperties.put(new AMQPString(entry.getKey()), convertToSwiftMqType(entry.getValue()));
}
try {
outboundMessage.setApplicationProperties(new ApplicationProperties(applicationProperties));
} catch (IOException e) {
throw new StreamException("Error while setting application properties", e);
}
}
if (message.getMessageAnnotations() != null && !message.getMessageAnnotations().isEmpty()) {
Map<AMQPType, AMQPType> messageAnnotations = new LinkedHashMap<>(message.getMessageAnnotations().size());
for (Map.Entry<String, Object> entry : message.getMessageAnnotations().entrySet()) {
messageAnnotations.put(new AMQPSymbol(entry.getKey()), convertToSwiftMqType(entry.getValue()));
}
try {
outboundMessage.setMessageAnnotations(new MessageAnnotations(messageAnnotations));
} catch (IOException e) {
throw new StreamException("Error while setting message annotations", e);
}
}
if (message.getBodyAsBinary() != null) {
outboundMessage.addData(new Data(message.getBodyAsBinary()));
}
}
try {
int bufferSize;
// FIXME estimate the size with all message data
if (outboundMessage.getData() != null && !outboundMessage.getData().isEmpty()) {
bufferSize = outboundMessage.getData().get(0).getPredictedSize();
} else {
bufferSize = 8192;
}
DataByteArrayOutputStream output = new DataByteArrayOutputStream(bufferSize);
outboundMessage.writeContent(output);
return new EncodedMessage(output.getCount(), output.getBuffer());
} catch (IOException e) {
throw new StreamException("Error while writing AMQP 1.0 message to output stream");
}
}
use of com.swiftmq.amqp.v100.generated.messaging.message_format.Properties in project swiftmq-ce by iitsoftware.
the class RequestorNonTransacted method main.
public static void main(String[] args) {
if (args.length == 1 && args[0].equals("?")) {
System.out.println();
System.out.println("Usage: <host> <port> <target> <nreq> <qos> <authanon> [<username> <password>]");
System.out.println(" <qos> ::= AT_LEAST_ONCE | AT_MOST_ONCE | EXACTLY_ONCE");
System.out.println(" Suppress <username> <password> and set <authanon> to false to avoid SASL.");
System.out.println();
System.exit(0);
}
String host = "localhost";
int port = 5672;
String target = "testqueue";
int nMsgs = 100;
String qosS = "EXACTLY_ONCE";
boolean authAnon = true;
String user = null;
String password = null;
if (args.length >= 1)
host = args[0];
if (args.length >= 2)
port = Integer.parseInt(args[1]);
if (args.length >= 3)
target = args[2];
if (args.length >= 4)
nMsgs = Integer.parseInt(args[3]);
if (args.length >= 5)
qosS = args[4];
if (args.length >= 6)
authAnon = Boolean.parseBoolean(args[5]);
if (args.length >= 7)
user = args[6];
if (args.length >= 8)
password = args[7];
System.out.println();
System.out.println("Host : " + host);
System.out.println("Port : " + port);
System.out.println("Target : " + target);
System.out.println("Number Req : " + nMsgs);
System.out.println("QoS : " + qosS);
System.out.println("Auth as Anon: " + authAnon);
System.out.println("User : " + user);
System.out.println("Password : " + password);
System.out.println();
try {
// Create connection and connect
AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT);
Connection connection = null;
if (args.length < 7)
connection = new Connection(ctx, host, port, authAnon);
else
connection = new Connection(ctx, host, port, user, password);
if (port == 5671) {
System.out.println("Using SSL on port 5671");
connection.setSocketFactory(new JSSESocketFactory());
}
connection.connect();
// Create session and producer
Session session = connection.createSession(50, 50);
Producer p = session.createProducer(target, toIntQoS(qosS));
// Create a temp queue and a consumer
Consumer c = session.createConsumer(100, toIntQoS(qosS));
AddressIF tempDest = c.getRemoteAddress();
// Do request / reply
for (int i = 0; i < nMsgs; i++) {
// Send the request
AMQPMessage request = new AMQPMessage();
Properties prop = new Properties();
prop.setReplyTo(tempDest);
request.setProperties(prop);
String s = "Message #" + (i + 1);
System.out.println("Sending " + s);
request.setAmqpValue(new AmqpValue(new AMQPString(s)));
p.send(request);
// Receive the reply
AMQPMessage reply = c.receive();
if (reply == null)
break;
if (!reply.isSettled())
reply.accept();
AmqpValue value = reply.getAmqpValue();
System.out.println("Received: " + ((AMQPString) value.getValue()).getValue());
}
Thread.sleep(2000);
// Close everything down
c.close();
p.close();
session.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.swiftmq.amqp.v100.generated.messaging.message_format.Properties in project swiftmq-client by iitsoftware.
the class EndpointImpl method performRequest.
public synchronized void performRequest(Request request) {
try {
dos.rewind();
Dumpalizer.dump(dos, request);
AMQPMessage msg = new AMQPMessage();
byte[] bytes = new byte[dos.getCount()];
System.arraycopy(dos.getBuffer(), 0, bytes, 0, bytes.length);
msg.addData(new Data(bytes));
Properties prop = new Properties();
prop.setReplyTo(replyAddress);
msg.setProperties(prop);
producer.send(msg);
} catch (Exception e) {
close();
}
}
use of com.swiftmq.amqp.v100.generated.messaging.message_format.Properties in project swiftmq-client by iitsoftware.
the class FlowFrame method decode.
private void decode() throws Exception {
List l = body.getValue();
AMQPType t = null;
int idx = 0;
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
try {
if (t.getCode() != AMQPTypeDecoder.NULL)
nextIncomingId = new TransferNumber(((AMQPUnsignedInt) t).getValue());
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'nextIncomingId' in 'Flow' frame: " + e);
}
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
if (t.getCode() == AMQPTypeDecoder.NULL)
throw new Exception("Mandatory field 'incomingWindow' in 'Flow' frame is NULL");
try {
incomingWindow = (AMQPUnsignedInt) t;
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'incomingWindow' in 'Flow' frame: " + e);
}
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
if (t.getCode() == AMQPTypeDecoder.NULL)
throw new Exception("Mandatory field 'nextOutgoingId' in 'Flow' frame is NULL");
try {
nextOutgoingId = new TransferNumber(((AMQPUnsignedInt) t).getValue());
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'nextOutgoingId' in 'Flow' frame: " + e);
}
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
if (t.getCode() == AMQPTypeDecoder.NULL)
throw new Exception("Mandatory field 'outgoingWindow' in 'Flow' frame is NULL");
try {
outgoingWindow = (AMQPUnsignedInt) t;
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'outgoingWindow' in 'Flow' frame: " + e);
}
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
try {
if (t.getCode() != AMQPTypeDecoder.NULL)
handle = new Handle(((AMQPUnsignedInt) t).getValue());
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'handle' in 'Flow' frame: " + e);
}
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
try {
if (t.getCode() != AMQPTypeDecoder.NULL)
deliveryCount = new SequenceNo(((AMQPUnsignedInt) t).getValue());
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'deliveryCount' in 'Flow' frame: " + e);
}
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
try {
if (t.getCode() != AMQPTypeDecoder.NULL)
linkCredit = (AMQPUnsignedInt) t;
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'linkCredit' in 'Flow' frame: " + e);
}
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
try {
if (t.getCode() != AMQPTypeDecoder.NULL)
available = (AMQPUnsignedInt) t;
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'available' in 'Flow' frame: " + e);
}
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
try {
if (t.getCode() != AMQPTypeDecoder.NULL)
drain = (AMQPBoolean) t;
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'drain' in 'Flow' frame: " + e);
}
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
try {
if (t.getCode() != AMQPTypeDecoder.NULL)
echo = (AMQPBoolean) t;
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'echo' in 'Flow' frame: " + e);
}
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
try {
if (t.getCode() != AMQPTypeDecoder.NULL)
properties = new Fields(((AMQPMap) t).getValue());
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'properties' in 'Flow' frame: " + e);
}
}
use of com.swiftmq.amqp.v100.generated.messaging.message_format.Properties in project swiftmq-client by iitsoftware.
the class OpenFrame method decode.
private void decode() throws Exception {
List l = body.getValue();
AMQPType t = null;
int idx = 0;
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
if (t.getCode() == AMQPTypeDecoder.NULL)
throw new Exception("Mandatory field 'containerId' in 'Open' frame is NULL");
try {
containerId = (AMQPString) t;
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'containerId' in 'Open' frame: " + e);
}
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
try {
if (t.getCode() != AMQPTypeDecoder.NULL)
hostname = (AMQPString) t;
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'hostname' in 'Open' frame: " + e);
}
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
try {
if (t.getCode() != AMQPTypeDecoder.NULL)
maxFrameSize = (AMQPUnsignedInt) t;
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'maxFrameSize' in 'Open' frame: " + e);
}
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
try {
if (t.getCode() != AMQPTypeDecoder.NULL)
channelMax = (AMQPUnsignedShort) t;
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'channelMax' in 'Open' frame: " + e);
}
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
try {
if (t.getCode() != AMQPTypeDecoder.NULL)
idleTimeOut = new Milliseconds(((AMQPUnsignedInt) t).getValue());
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'idleTimeOut' in 'Open' frame: " + e);
}
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
try {
if (t.getCode() != AMQPTypeDecoder.NULL)
outgoingLocales = AMQPTypeDecoder.isArray(t.getCode()) ? (AMQPArray) t : singleToArray(t);
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'outgoingLocales' in 'Open' frame: " + e);
}
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
try {
if (t.getCode() != AMQPTypeDecoder.NULL)
incomingLocales = AMQPTypeDecoder.isArray(t.getCode()) ? (AMQPArray) t : singleToArray(t);
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'incomingLocales' in 'Open' frame: " + e);
}
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
try {
if (t.getCode() != AMQPTypeDecoder.NULL)
offeredCapabilities = AMQPTypeDecoder.isArray(t.getCode()) ? (AMQPArray) t : singleToArray(t);
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'offeredCapabilities' in 'Open' frame: " + e);
}
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
try {
if (t.getCode() != AMQPTypeDecoder.NULL)
desiredCapabilities = AMQPTypeDecoder.isArray(t.getCode()) ? (AMQPArray) t : singleToArray(t);
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'desiredCapabilities' in 'Open' frame: " + e);
}
// Factory : ./.
if (idx >= l.size())
return;
t = (AMQPType) l.get(idx++);
try {
if (t.getCode() != AMQPTypeDecoder.NULL)
properties = new Fields(((AMQPMap) t).getValue());
} catch (ClassCastException e) {
throw new Exception("Invalid type of field 'properties' in 'Open' frame: " + e);
}
}
Aggregations