Example usage for org.springframework.amqp.core MessageProperties setDeliveryMode

List of usage examples for org.springframework.amqp.core MessageProperties setDeliveryMode

Introduction

In this page you can find the example usage for org.springframework.amqp.core MessageProperties setDeliveryMode.

Prototype

public void setDeliveryMode(MessageDeliveryMode deliveryMode) 

Source Link

Usage

From source file:weChat.amqp.RpcTest.java

/**
 * Sends a message to a service that upcases the String and returns as a
 * reply using a {@link RabbitTemplate} configured with a fixed reply queue
 * and reply listener, configured with JavaConfig.
 *//*from  w w  w  .j  av  a2 s . co m*/
@Test
public void test() {
    AmqpReqParam param = new AmqpReqParam();
    param.setCmdid("WJ007");
    param.setCompanycode("01103");
    param.setWechatpubinfoid(1);
    BaseDto dto = new BaseDto();
    dto.put("cardnum", "5000011");
    param.setParams(dto);
    //String corrId = UUID.randomUUID().toString();
    String corrId = FixedReplyQueueConfig.responseQueue;
    Object resp = rabbitTemplate.convertSendAndReceive(FixedReplyQueueConfig.routing, param, (message) -> {
        MessageProperties properities = message.getMessageProperties();

        System.out.println("corrId:" + corrId);
        properities.setCorrelationId(corrId.getBytes());
        properities.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        properities.setTimestamp(new Date());
        long currentTimeMillis = System.currentTimeMillis();
        properities.setMessageId(String.valueOf(currentTimeMillis));
        properities.setExpiration(String.valueOf(50000));
        properities.setContentEncoding(corrId);

        return message;
    });
    System.out.println("?" + resp);
}

From source file:weChat.amqp.JavaConfigFixedReplyQueueTests.java

/**
 * Sends a message to a service that upcases the String and returns as a
 * reply using a {@link RabbitTemplate} configured with a fixed reply queue
 * and reply listener, configured with JavaConfig.
 *//*from w w w .j  a  va 2  s .c o m*/
@Test
public void test() {
    AmqpReqParam param = new AmqpReqParam();
    param.setCmdid("WJ007");
    param.setCompanycode("00111");
    param.setWechatpubinfoid(1);
    BaseDto dto = new BaseDto();
    dto.put("cardnum", "5000028");
    param.setParams(dto);
    AmqpRespParam resp = (AmqpRespParam) rabbitTemplate.convertSendAndReceive(param, (message) -> {
        MessageProperties properities = message.getMessageProperties();
        String corrId = UUID.randomUUID().toString();
        properities.setCorrelationId(corrId.getBytes());
        properities.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        properities.setTimestamp(new Date());
        long currentTimeMillis = System.currentTimeMillis();
        properities.setMessageId(String.valueOf(currentTimeMillis));

        return message;
    });
    System.out.println(resp);
}

From source file:org.springframework.amqp.rabbit.core.BatchingRabbitTemplateTests.java

@Test
public void testDebatchByContainerPerformance() throws Exception {
    final List<Message> received = new ArrayList<Message>();
    int count = 100000;
    final CountDownLatch latch = new CountDownLatch(count);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(this.connectionFactory);
    container.setQueueNames(ROUTE);// w w w.java  2 s .co  m
    container.setMessageListener((MessageListener) message -> {
        received.add(message);
        latch.countDown();
    });
    container.setReceiveTimeout(100);
    container.setPrefetchCount(1000);
    container.setTxSize(1000);
    container.afterPropertiesSet();
    container.start();
    try {
        BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(1000, Integer.MAX_VALUE, 30000);
        BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
        //         RabbitTemplate template = new RabbitTemplate();
        template.setConnectionFactory(this.connectionFactory);
        MessageProperties props = new MessageProperties();
        props.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        Message message = new Message(new byte[256], props);
        StopWatch watch = new StopWatch();
        watch.start();
        for (int i = 0; i < count; i++) {
            template.send("", ROUTE, message);
        }
        assertTrue(latch.await(60, TimeUnit.SECONDS));
        watch.stop();
        // System .out .println(watch.getTotalTimeMillis());
        assertEquals(count, received.size());
    } finally {
        container.stop();
    }
}

From source file:org.springframework.amqp.rabbit.support.RabbitUtils.java

public static MessageProperties createMessageProperties(final BasicProperties source, final Envelope envelope,
        final String charset) {
    MessageProperties target = new MessageProperties();
    Map<String, Object> headers = source.getHeaders();
    if (!CollectionUtils.isEmpty(headers)) {
        for (Map.Entry<String, Object> entry : headers.entrySet()) {
            target.setHeader(entry.getKey(), entry.getValue());
        }/*  ww w. jav  a 2 s.  co  m*/
    }
    target.setTimestamp(source.getTimestamp());
    target.setMessageId(source.getMessageId());
    target.setUserId(source.getUserId());
    target.setAppId(source.getAppId());
    target.setClusterId(source.getClusterId());
    target.setType(source.getType());
    Integer deliverMode = source.getDeliveryMode();
    if (deliverMode != null) {
        target.setDeliveryMode(MessageDeliveryMode.fromInt(deliverMode));
    }
    target.setExpiration(source.getExpiration());
    target.setPriority(source.getPriority());
    target.setContentType(source.getContentType());
    target.setContentEncoding(source.getContentEncoding());
    String correlationId = source.getCorrelationId();
    if (correlationId != null) {
        try {
            target.setCorrelationId(source.getCorrelationId().getBytes(charset));
        } catch (UnsupportedEncodingException ex) {
            throw new AmqpUnsupportedEncodingException(ex);
        }
    }
    String replyTo = source.getReplyTo();
    if (replyTo != null) {
        target.setReplyTo(new Address(replyTo));
    }
    if (envelope != null) {
        target.setReceivedExchange(envelope.getExchange());
        target.setReceivedRoutingKey(envelope.getRoutingKey());
        target.setRedelivered(envelope.isRedeliver());
        target.setDeliveryTag(envelope.getDeliveryTag());
    }
    // TODO: what about messageCount?
    return target;
}