當前位置: 首頁>>代碼示例>>Java>>正文


Java RabbitTemplate.convertAndSend方法代碼示例

本文整理匯總了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;
}
 
開發者ID:oncecloud,項目名稱:devops-cstack,代碼行數:26,代碼來源:CheckBrokerConnectionUtils.java

示例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);
}
 
開發者ID:huangye177,項目名稱:spring4probe,代碼行數:23,代碼來源:RabbitMQApp.java

示例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);
	}
}
 
開發者ID:labcabrera,項目名稱:lab-insurance,代碼行數:35,代碼來源:InsuranceTaskExecutor.java

示例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 });
}
 
開發者ID:mixaceh,項目名稱:openyu-commons,代碼行數:9,代碼來源:ApplicationContextRabbitmqTest.java

示例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();
}
 
開發者ID:spring-cloud,項目名稱:spring-cloud-stream-binder-rabbit,代碼行數:42,代碼來源:RabbitBinderTests.java

示例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();
}
 
開發者ID:spring-cloud,項目名稱:spring-cloud-stream-binder-rabbit,代碼行數:46,代碼來源:RabbitBinderTests.java

示例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();
}
 
開發者ID:spring-cloud,項目名稱:spring-cloud-stream-binder-rabbit,代碼行數:48,代碼來源:RabbitBinderTests.java

示例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));
}
 
開發者ID:openbaton,項目名稱:NFVO,代碼行數:20,代碼來源:Registration.java


注:本文中的org.springframework.amqp.rabbit.core.RabbitTemplate.convertAndSend方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。