net.sf.aguacate.cloud.messaging.rabbitmq.MessagingBridgeRabbitmq Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aguacate-cloud-rabbitmq Show documentation
Show all versions of aguacate-cloud-rabbitmq Show documentation
Aguacate RabbitMQ Cloud Project
The newest version!
package net.sf.aguacate.cloud.messaging.rabbitmq;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import net.sf.aguacate.cloud.messaging.MessagingBridge;
public class MessagingBridgeRabbitmq implements MessagingBridge {
private final ConnectionFactory factory;
MessagingBridgeRabbitmq(Map configuration) {
@SuppressWarnings("unchecked")
Map rabbitmq = (Map) configuration.get("rabbitmq");
ConnectionFactory tmp = new ConnectionFactory();
tmp.setUsername((String) rabbitmq.get("username"));
tmp.setPassword((String) rabbitmq.get("password"));
tmp.setVirtualHost((String) rabbitmq.get("vitualhost"));
tmp.setHost((String) rabbitmq.get("host"));
String port = (String) rabbitmq.get("port");
if (port != null) {
tmp.setPort(Integer.parseInt((String) port));
}
factory = tmp;
}
public void sendStringMessage(String message) {
try {
Connection conn = factory.newConnection();
try {
Channel channel = conn.createChannel();
try {
channel.basicPublish("amq.direct", "key001", false,
new BasicProperties().builder().deliveryMode(2).contentEncoding("string").build(),
message.getBytes(StandardCharsets.UTF_8));
} finally {
try {
channel.close();
} catch (IOException | TimeoutException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException | TimeoutException e) {
e.printStackTrace();
}
}
}