本文整理汇总了Java中org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend方法的典型用法代码示例。如果您正苦于以下问题:Java RabbitTemplate.convertAndSend方法的具体用法?Java RabbitTemplate.convertAndSend怎么用?Java RabbitTemplate.convertAndSend使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.amqp.rabbit.core.RabbitTemplate
的用法示例。
在下文中一共展示了RabbitTemplate.convertAndSend方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkRabbitMQAMQPProtocol
import org.springframework.amqp.rabbit.core.RabbitTemplate; //导入方法依赖的package包/类
public String checkRabbitMQAMQPProtocol(String messageAsString,
String url,
String user,
String password,
String vhost) throws Exception {
org.springframework.amqp.rabbit.connection.CachingConnectionFactory cf =
new org.springframework.amqp.rabbit.connection.CachingConnectionFactory();
URL formattedUrl = new URL("http://" + url);
cf.setHost(formattedUrl.getHost());
cf.setPort(formattedUrl.getPort());
cf.setUsername(user);
cf.setPassword(password);
cf.setVirtualHost(vhost);
RabbitAdmin admin = new RabbitAdmin(cf);
org.springframework.amqp.core.Queue queue = new org.springframework.amqp.core.Queue("myQueue");
admin.declareQueue(queue);
TopicExchange exchange = new TopicExchange("myExchange");
admin.declareExchange(exchange);
admin.declareBinding(
BindingBuilder.bind(queue).to(exchange).with("foo.*"));
RabbitTemplate template = new RabbitTemplate(cf);
template.convertAndSend("myExchange", "foo.bar", messageAsString);
String receivedMessage = template.receiveAndConvert("myQueue").toString();
return receivedMessage;
}
示例2: main
import org.springframework.amqp.rabbit.core.RabbitTemplate; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception
{
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
// invoke the CONFIG
/*
* application context then starts the message listener container, and
* the message listener container bean starts listening for messages
*/
ctx.register(RabbitMQConfig.class);
ctx.refresh();
RabbitTemplate rabbitTemplate = ctx.getBean(RabbitTemplate.class);
System.out.println("* Waiting 3 seconds...");
Thread.sleep(3000);
System.out.println("* Sending message...");
rabbitTemplate.convertAndSend(queueName, "* Hello from RabbitMQ!");
System.exit(0);
}
示例3: testConnect
import org.springframework.amqp.rabbit.core.RabbitTemplate; //导入方法依赖的package包/类
@Test
public void testConnect() {
RabbitAdmin admin = new RabbitAdmin(this.lqcf);
Queue queue = new Queue(UUID.randomUUID().toString(), false, false, true);
admin.declareQueue(queue);
ConnectionFactory targetConnectionFactory = this.lqcf.getTargetConnectionFactory("[" + queue.getName() + "]");
RabbitTemplate template = new RabbitTemplate(targetConnectionFactory);
template.convertAndSend("", queue.getName(), "foo");
assertThat(template.receiveAndConvert(queue.getName())).isEqualTo("foo");
((CachingConnectionFactory) targetConnectionFactory).destroy();
admin.deleteQueue(queue.getName());
}
开发者ID:spring-cloud,项目名称:spring-cloud-stream-binder-rabbit,代码行数:13,代码来源:LocalizedQueueConnectionFactoryIntegrationTests.java
示例4: internalExecute
import org.springframework.amqp.rabbit.core.RabbitTemplate; //导入方法依赖的package包/类
private void internalExecute(InsuranceTask task) throws JsonProcessingException {
Assert.notNull(task.getData(), "Missing task data");
String routingKey = task.getDestinationQueue();
Object data = task.getData();
String message = mapper.writeValueAsString(data);
try {
log.info("Sending message to {}", routingKey);
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setReplyTimeout(5000);
rabbitTemplate.setReceiveTimeout(5000);
rabbitTemplate.setQueue(routingKey);
// TODO
boolean sync = false;
if (sync) {
Object response = rabbitTemplate.convertSendAndReceive(routingKey, message);
log.info("Received message from {}: {}", routingKey, response);
}
else {
rabbitTemplate.convertAndSend(routingKey, message);
try {
Thread.sleep(5000);
}
catch (InterruptedException e) {
}
}
}
catch (RuntimeException ex) {
log.error("Task execution error using routingKey {}", routingKey);
throw new InsuranceException("Task execution error", ex);
}
}
示例5: send
import org.springframework.amqp.rabbit.core.RabbitTemplate; //导入方法依赖的package包/类
@Test
public void send() throws Exception {
RabbitTemplate bean = (RabbitTemplate) applicationContext
.getBean("rabbitTemplate");
//
String QUEUE_NAME = "TEST_BENCHMARK";
bean.convertAndSend(QUEUE_NAME, new byte[] { 1, 2, 3 });
}
示例6: testDurablePubSubWithAutoBindDLQ
import org.springframework.amqp.rabbit.core.RabbitTemplate; //导入方法依赖的package包/类
@Test
public void testDurablePubSubWithAutoBindDLQ() throws Exception {
RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());
RabbitTestBinder binder = getBinder();
ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties();
consumerProperties.getExtension().setPrefix(TEST_PREFIX);
consumerProperties.getExtension().setAutoBindDlq(true);
consumerProperties.getExtension().setDurableSubscription(true);
consumerProperties.setMaxAttempts(1); // disable retry
DirectChannel moduleInputChannel = createBindableChannel("input", createConsumerBindingProperties(consumerProperties));
moduleInputChannel.setBeanName("durableTest");
moduleInputChannel.subscribe(new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
throw new RuntimeException("foo");
}
});
Binding<MessageChannel> consumerBinding = binder.bindConsumer("durabletest.0", "tgroup", moduleInputChannel,
consumerProperties);
RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource());
template.convertAndSend(TEST_PREFIX + "durabletest.0", "", "foo");
int n = 0;
while (n++ < 100) {
Object deadLetter = template.receiveAndConvert(TEST_PREFIX + "durabletest.0.tgroup.dlq");
if (deadLetter != null) {
assertThat(deadLetter).isEqualTo("foo");
break;
}
Thread.sleep(100);
}
assertThat(n).isLessThan(100);
consumerBinding.unbind();
assertThat(admin.getQueueProperties(TEST_PREFIX + "durabletest.0.tgroup.dlq")).isNotNull();
}
示例7: testAutoBindDLQ
import org.springframework.amqp.rabbit.core.RabbitTemplate; //导入方法依赖的package包/类
@Test
public void testAutoBindDLQ() throws Exception {
RabbitTestBinder binder = getBinder();
ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties();
consumerProperties.getExtension().setPrefix(TEST_PREFIX);
consumerProperties.getExtension().setAutoBindDlq(true);
consumerProperties.setMaxAttempts(1); // disable retry
consumerProperties.getExtension().setDurableSubscription(true);
BindingProperties bindingProperties = createConsumerBindingProperties(consumerProperties);
DirectChannel moduleInputChannel = createBindableChannel("input", bindingProperties);
moduleInputChannel.setBeanName("dlqTest");
moduleInputChannel.subscribe(new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
throw new RuntimeException("foo");
}
});
Binding<MessageChannel> consumerBinding = binder.bindConsumer("dlqtest", "default", moduleInputChannel,
consumerProperties);
RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource());
template.convertAndSend("", TEST_PREFIX + "dlqtest.default", "foo");
int n = 0;
while (n++ < 100) {
Object deadLetter = template.receiveAndConvert(TEST_PREFIX + "dlqtest.default.dlq");
if (deadLetter != null) {
assertThat(deadLetter).isEqualTo("foo");
break;
}
Thread.sleep(100);
}
assertThat(n).isLessThan(100);
consumerBinding.unbind();
ApplicationContext context = TestUtils.getPropertyValue(binder, "binder.provisioningProvider.autoDeclareContext",
ApplicationContext.class);
assertThat(context.containsBean(TEST_PREFIX + "dlqtest.default.binding")).isFalse();
assertThat(context.containsBean(TEST_PREFIX + "dlqtest.default")).isFalse();
assertThat(context.containsBean(TEST_PREFIX + "dlqtest.default.dlq.binding")).isFalse();
assertThat(context.containsBean(TEST_PREFIX + "dlqtest.default.dlq")).isFalse();
}
示例8: testAutoBindDLQwithRepublish
import org.springframework.amqp.rabbit.core.RabbitTemplate; //导入方法依赖的package包/类
@Test
public void testAutoBindDLQwithRepublish() throws Exception {
// pre-declare the queue with dead-lettering, users can also use a policy
RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-dead-letter-exchange", TEST_PREFIX + "DLX");
args.put("x-dead-letter-routing-key", TEST_PREFIX + "dlqpubtest.default");
Queue queue = new Queue(TEST_PREFIX + "dlqpubtest.default", true, false, false, args);
admin.declareQueue(queue);
RabbitTestBinder binder = getBinder();
ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties();
consumerProperties.getExtension().setPrefix(TEST_PREFIX);
consumerProperties.getExtension().setAutoBindDlq(true);
consumerProperties.getExtension().setRepublishToDlq(true);
consumerProperties.setMaxAttempts(1); // disable retry
consumerProperties.getExtension().setDurableSubscription(true);
DirectChannel moduleInputChannel = createBindableChannel("input", createConsumerBindingProperties(consumerProperties));
moduleInputChannel.setBeanName("dlqPubTest");
moduleInputChannel.subscribe(new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
throw new RuntimeException("foo");
}
});
Binding<MessageChannel> consumerBinding = binder.bindConsumer("foo.dlqpubtest", "foo", moduleInputChannel,
consumerProperties);
RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource());
template.convertAndSend("", TEST_PREFIX + "foo.dlqpubtest.foo", "foo");
int n = 0;
while (n++ < 100) {
org.springframework.amqp.core.Message deadLetter = template.receive(TEST_PREFIX + "foo.dlqpubtest.foo.dlq");
if (deadLetter != null) {
assertThat(new String(deadLetter.getBody())).isEqualTo("foo");
assertThat(deadLetter.getMessageProperties().getHeaders()).containsKey(("x-exception-stacktrace"));
break;
}
Thread.sleep(100);
}
assertThat(n).isLessThan(100);
consumerBinding.unbind();
}
示例9: deregisterVnfmFromNfvo
import org.springframework.amqp.rabbit.core.RabbitTemplate; //导入方法依赖的package包/类
/**
* This method deregisters a Vnfm from the Nfvo by sending a request to the nfvo.manager.handling
* queue using the given RabbitTemplate. The rabbitTemplate object should be obtained from the
* Vnfm's VnfmSpringHelperRabbit object.
*
* @param rabbitTemplate The spring rabbit template @see
* org.springframework.amqp.rabbit.core.RabbitTemplate
* @param endpoint the VNfm Endpoint to remove @see
* org.openbaton.catalogue.nfvo.VnfmManagerEndpoint
*/
public void deregisterVnfmFromNfvo(RabbitTemplate rabbitTemplate, VnfmManagerEndpoint endpoint) {
JsonObject message = new JsonObject();
message.add("username", new JsonPrimitive(this.username));
message.add("action", new JsonPrimitive("deregister"));
message.add("password", new JsonPrimitive(this.password));
message.add("vnfmManagerEndpoint", gson.toJsonTree(endpoint));
log.debug("Deregister the Vnfm from the Nfvo");
rabbitTemplate.convertAndSend("nfvo.manager.handling", gson.toJson(message));
}