本文整理汇总了Java中com.rabbitmq.client.AMQP类的典型用法代码示例。如果您正苦于以下问题:Java AMQP类的具体用法?Java AMQP怎么用?Java AMQP使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AMQP类属于com.rabbitmq.client包,在下文中一共展示了AMQP类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: buildChildSpan
import com.rabbitmq.client.AMQP; //导入依赖的package包/类
static Scope buildChildSpan(AMQP.BasicProperties props, Tracer tracer) {
SpanContext context = TracingUtils.extract(props, tracer);
if (context != null) {
Tracer.SpanBuilder spanBuilder = tracer.buildSpan("receive")
.ignoreActiveSpan()
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CONSUMER);
spanBuilder.addReference(References.FOLLOWS_FROM, context);
Scope scope = spanBuilder.startActive(true);
SpanDecorator.onResponse(scope.span());
return scope;
}
return null;
}
示例2: inject
import com.rabbitmq.client.AMQP; //导入依赖的package包/类
private AMQP.BasicProperties inject(AMQP.BasicProperties properties, Span span) {
// Headers of AMQP.BasicProperties is unmodifiableMap therefore we build new AMQP.BasicProperties
// with injected span context into headers
Map<String, Object> headers = new HashMap<>();
tracer.inject(span.context(), Format.Builtin.TEXT_MAP, new HeadersMapInjectAdapter(headers));
if (properties == null) {
return new AMQP.BasicProperties().builder().headers(headers).build();
}
if (properties.getHeaders() != null) {
headers.putAll(properties.getHeaders());
}
return properties.builder()
.headers(headers)
.build();
}
示例3: publishAsync
import com.rabbitmq.client.AMQP; //导入依赖的package包/类
public ListenableFuture<?> publishAsync(final Exchange exchange, final Message message, final @Nullable BasicProperties properties, final @Nullable Publish publish) {
// NOTE: Serialization must happen synchronously, because getter methods may not be thread-safe
final String payload = gson.toJson(message);
final AMQP.BasicProperties finalProperties = getProperties(message, properties);
final Publish finalPublish = Publish.forMessage(message, publish);
if(this.executorService == null) throw new IllegalStateException("Not connected");
return this.executorService.submit(new Runnable() {
@Override
public void run() {
try {
publish(exchange, payload, finalProperties, finalPublish);
} catch(Throwable e) {
logger.log(Level.SEVERE, "Unhandled exception publishing message type " + finalProperties.getType(), e);
}
}
});
}
示例4: sourceRecord
import com.rabbitmq.client.AMQP; //导入依赖的package包/类
SourceRecord sourceRecord(String consumerTag, Envelope envelope, AMQP.BasicProperties basicProperties, byte[] bytes) {
Struct key = MessageConverter.key(basicProperties);
Struct value = MessageConverter.value(consumerTag, envelope, basicProperties, bytes);
final String topic = this.config.kafkaTopic.execute(RabbitMQSourceConnectorConfig.KAFKA_TOPIC_TEMPLATE, value);
return new SourceRecord(
ImmutableMap.of("routingKey", envelope.getRoutingKey()),
ImmutableMap.of("deliveryTag", envelope.getDeliveryTag()),
topic,
null,
key.schema(),
key,
value.schema(),
value,
null == basicProperties.getTimestamp() ? this.time.milliseconds() : basicProperties.getTimestamp().getTime()
);
}
示例5: GabrielGateway
import com.rabbitmq.client.AMQP; //导入依赖的package包/类
GabrielGateway(int shardId, Channel channel) throws IOException {
super(shardId, channel);
channel.queueDeclare("shard-" + shardId + "-getping", false, false, false, null);
channel.queueDeclare("shard-" + shardId + "-getping-response", false, false, false, null);
channel.basicConsume("shard-" + shardId + "-getping", true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
channel.basicPublish("", "shard-" + shardId + "-getping-response", null, body);
}
});
}
示例6: handleDelivery
import com.rabbitmq.client.AMQP; //导入依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
// decode internal message
InternalMessage msg = JSONs.decodeInternalMessage(body);
// notify listener
if (msg != null) {
logger.debug("Communicator received: Received {} message for client {} user {}", msg.getMessageType(), msg.getClientId(), msg.getUserName());
switch (msg.getMessageType()) {
case PUBLISH:
listener.onPublish((InternalMessage<Publish>) msg);
break;
case DISCONNECT:
listener.onDisconnect((InternalMessage<Disconnect>) msg);
break;
default:
logger.warn("Communicator error: Communicator received unexpected message type {}", msg.getMessageType());
}
}
}
示例7: main
import com.rabbitmq.client.AMQP; //导入依赖的package包/类
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @throws InterruptedException
* @throws ConsumerCancelledException
* @throws ShutdownSignalException
*/
public static void main(String[] args) throws IOException, TimeoutException, ShutdownSignalException,
ConsumerCancelledException, InterruptedException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);
channel.basicQos(1);
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(RPC_QUEUE_NAME, false, consumer);
System.out.println("RPCServer Awating RPC request");
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
BasicProperties props = delivery.getProperties();
BasicProperties replyProps = new AMQP.BasicProperties.Builder().correlationId(props.getCorrelationId())
.build();
String message = new String(delivery.getBody(), "UTF-8");
int n = Integer.parseInt(message);
System.out.println("RPCServer fib(" + message + ")");
String response = "" + fib(n);
channel.basicPublish("", props.getReplyTo(), replyProps, response.getBytes());
channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
}
}
示例8: main
import com.rabbitmq.client.AMQP; //导入依赖的package包/类
/**
* @param args
* @throws IOException
* @throws TimeoutException
* @date 2017年7月13日 下午2:57:32
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 声明交换器
channel.exchangeDeclare(EXCHANGE_NAME_ROUTING, "direct");
// 获取匿名队列名称
String queueName = channel.queueDeclare().getQueue();
// 根据路由关键字进行多重绑定
for (String severity : routingKeys2) {
channel.queueBind(queueName, EXCHANGE_NAME_ROUTING, severity);
System.out.println("ReceiveLogsDirect2 exchange:" + EXCHANGE_NAME_ROUTING + ", queue:" + queueName
+ ", BindRoutingKey:" + severity);
}
System.out.println("ReceiveLogsDirect2 Waiting for messages");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws UnsupportedEncodingException {
String message = new String(body, "UTF-8");
System.out.println("ReceiveLogsDirect2 Received '" + envelope.getRoutingKey() + "':'" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);
}
示例9: main
import com.rabbitmq.client.AMQP; //导入依赖的package包/类
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @date 2017年7月13日 下午2:53:18
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 声明交换器
channel.exchangeDeclare(EXCHANGE_NAME_ROUTING, "direct");
// 获取匿名队列名称
String queueName = channel.queueDeclare().getQueue();
// 根据路由关键字进行绑定
for (String routingKey : routingKeys1) {
channel.queueBind(queueName, EXCHANGE_NAME_ROUTING, routingKey);
System.out.println("ReceiveLogsDirect1 exchange:" + EXCHANGE_NAME_ROUTING + "," + " queue:" + queueName
+ ", BindRoutingKey:" + routingKey);
}
System.out.println("ReceiveLogsDirect1 Waiting for messages");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println("ReceiveLogsDirect1 Received '" + envelope.getRoutingKey() + "':'" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);
}
示例10: init
import com.rabbitmq.client.AMQP; //导入依赖的package包/类
public static synchronized void init(Channel channel) throws IOException, TimeoutException {
if(current != null) {
throw new IllegalStateException("Already started");
}
current = new GatewayInfo("unknown", "unknown", -1, -1, -1, -1, -1, -1, -1);
channel.queueDeclare("gateway-info", false, false, false, null);
channel.basicConsume("gateway-info", true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
JSONObject object = new JSONObject(new String(body, StandardCharsets.UTF_8));
JSONObject ram = object.getJSONObject("ram");
try {
current = new GatewayInfo(
object.getString("version"),
object.getString("jda-version"),
object.getDouble("cpu-usage"),
object.getInt("thread-count"),
object.getLong("uptime"),
ram.getLong("used"),
ram.getLong("free"),
ram.getLong("total"),
ram.getLong("max")
);
} catch(JSONException e) {
GabrielBot.LOGGER.error("Error creating GatewayInfo: " + e.getMessage());
}
}
});
}
示例11: main
import com.rabbitmq.client.AMQP; //导入依赖的package包/类
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @date 2017年7月13日 下午2:40:52
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
// 产生一个随机的队列名称
String queueName = channel.queueDeclare().getQueue();
channel.queueBind(queueName, EXCHANGE_NAME, "");// 对队列进行绑定
System.out.println("ReceiveLogs1 Waiting for messages");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println("ReceiveLogs1 Received '" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);// 队列会自动删除
}
示例12: main
import com.rabbitmq.client.AMQP; //导入依赖的package包/类
/**
* @param args
* @throws TimeoutException
* @throws IOException
* @date 2017年7月13日 下午3:08:40
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 声明一个匹配模式的交换器
channel.exchangeDeclare(EXCHANGE_NAME_TOPIC, "topic");
String queueName = channel.queueDeclare().getQueue();
// 路由关键字
String[] routingKeys = new String[]{"*.*.rabbit", "lazy.#"};
// 绑定路由关键字
for (String bindingKey : routingKeys) {
channel.queueBind(queueName, EXCHANGE_NAME_TOPIC, bindingKey);
System.out.println("ReceiveLogsTopic2 exchange:"+EXCHANGE_NAME_TOPIC+", queue:"+queueName+", BindRoutingKey:" + bindingKey);
}
System.out.println("ReceiveLogsTopic2 Waiting for messages");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws UnsupportedEncodingException {
String message = new String(body, "UTF-8");
System.out.println("ReceiveLogsTopic2 Received '" + envelope.getRoutingKey() + "':'" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);
}
示例13: main
import com.rabbitmq.client.AMQP; //导入依赖的package包/类
/**
* @param args
* @throws IOException
* @throws TimeoutException
* @date 2017年7月13日 下午3:06:20
* @writer junehappylove
*/
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost(host);
factory.setUsername(username);
factory.setPassword(password);
factory.setPort(port);
factory.setVirtualHost(virtualHost);
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 声明一个匹配模式的交换机
channel.exchangeDeclare(EXCHANGE_NAME_TOPIC, "topic");
String queueName = channel.queueDeclare().getQueue();
// 路由关键字
String[] routingKeys = new String[] { "*.orange.*" };
// 绑定路由
for (String routingKey : routingKeys) {
channel.queueBind(queueName, EXCHANGE_NAME_TOPIC, routingKey);
System.out.println("ReceiveLogsTopic1 exchange:" + EXCHANGE_NAME_TOPIC + ", queue:" + queueName
+ ", BindRoutingKey:" + routingKey);
}
System.out.println("ReceiveLogsTopic1 Waiting for messages");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println("ReceiveLogsTopic1 Received '" + envelope.getRoutingKey() + "':'" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);
}
示例14: configure
import com.rabbitmq.client.AMQP; //导入依赖的package包/类
/**
* Configures the rabbitmq queues and the {@code shard-<id>-receive} consumer.
*
* Subclasses that want to change how messages are sent/received can override this method to disable the default implementation.
*
* @throws IOException if there's an error on {@link Channel#queueDeclare(String, boolean, boolean, boolean, Map)} or {@link Channel#basicConsume(String, boolean, Consumer)}.
*/
protected void configure() throws IOException {
channel.queueDeclare("shard-" + shardId + "-send", true, false, false, null);
channel.queueDeclare("shard-" + shardId + "-receive", true, false, false, null);
channel.queueDeclare("shard-" + shardId + "-available", true, false, false, null);
channel.queueDeclare("shard-" + shardId + "-unavailable", true, false, false, null);
channel.basicConsume("shard-" + shardId + "-receive", true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
ClientWebSocketClient c = client.get();
if(c == null) return;
JSONObject obj = new JSONObject(new String(body, StandardCharsets.UTF_8));
if(obj.has("t") && obj.getString("t").equals("gateway-ping-update")) {
c.getJDA().setPing(obj.getJSONObject("d").getLong("ping"));
return;
}
if(enableRawGatewayEvent)
c.getJDA().getEventManager().handle(new RawGatewayEvent(c.getJDA(), new JSONObject(obj.toString())));
c.handleEvent(obj);
}
});
}
示例15: receiveQuickstartMessage
import com.rabbitmq.client.AMQP; //导入依赖的package包/类
/**
* 接受消息
*/
public void receiveQuickstartMessage() throws IOException, TimeoutException {
RabbitMQChannel channel = new RabbitMQChannel().channel();
AMQP.Queue.DeclareOk declareOk = channel.getChannel().queueDeclare(QUICKSTART_QUEUE_NAME, false, false, false, null);
System.out.println("等待接受队列【" + QUICKSTART_QUEUE_NAME + "】消息");
//建立一个消费者 监听消息的接受
Consumer consumer = new DefaultConsumer(channel.getChannel()) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body)
throws IOException {
String message = new String(body, "UTF-8");
System.out.println("接受消息:" + message);
}
};
channel.getChannel().basicConsume(QUICKSTART_QUEUE_NAME, true, consumer);
//channel.close();
}