欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

rabbitmqtemplate實現(xiàn)發(fā)送消息

前言

成都創(chuàng)新互聯(lián)是專業(yè)的藍田網(wǎng)站建設(shè)公司,藍田接單;提供網(wǎng)站建設(shè)、網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行藍田網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

rabbitmq template:消息模板。這是spring整合rabbit提供的消息模板。是進行發(fā)送消息的關(guān)鍵類。該類提供了豐富的發(fā)送方法,包括可靠性投遞消息方法、回調(diào)監(jiān)聽消息接口ConfirmCallback、返回值確認(rèn)接口ReturnCallBack等等。同樣我們需要注入到spring容器中,然后就可以想其他bean那樣正常使用了。

之前的配置是這樣的:

@Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);

        template.setMessageConverter(new Jackson2JsonMessageConverter());
        template.setMandatory(true);

        ...
        return template;
    }

要發(fā)送出去的消息vo是這樣的:

@Data
public class TestVO {
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date testDate;
}

然后,出現(xiàn)的問題就是,消息體里,時間比當(dāng)前時間少了8個小時。

{"testDate":"2019-12-27 05:45:26"}

原因

我們是這么使用rabbitmq template的:

@Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private redisRepository redisRepository;

    /**
     * 發(fā)送消息
     * @param exchange 交換機名稱
     * @param routingKey 路由鍵
     * @param msgMbject 消息體,無需序列化,會自動序列化為json
     */
    public void send(String exchange, String routingKey, final Object msgMbject) {
        CorrelationData correlationData = new CorrelationData(GUID.generate());
        CachedMqMessageForConfirm cachedMqMessageForConfirm = new CachedMqMessageForConfirm(exchange, routingKey, msgMbject);
        redisRepository.saveCacheMessageForConfirms(correlationData,cachedMqMessageForConfirm);
        //核心代碼:這里,發(fā)送出去的msgObject其實就是一個vo或者dto,rabbitmqTemplate會自動幫我們轉(zhuǎn)為json
        rabbitTemplate.convertAndSend(exchange,routingKey,msgMbject,correlationData);
    }

注釋里我解釋了,rabbitmq會自動做轉(zhuǎn)換,轉(zhuǎn)換用的就是jackson。

跟進源碼也能一探究竟:

org.springframework.amqp.rabbit.core.RabbitTemplate#convertAndSend

    @Override
    public void convertAndSend(String exchange, String routingKey, final Object object,
            @Nullable CorrelationData correlationData) throws AmqpException {
        // 這里調(diào)用了convertMessageIfNecessary(object)
        send(exchange, routingKey, convertMessageIfNecessary(object), correlationData);
    }
調(diào)用了convertMessageIfNessary:

protected Message convertMessageIfNecessary(final Object object) {
        if (object instanceof Message) {
            return (Message) object;
        }
        // 獲取消息轉(zhuǎn)換器
        return getRequiredMessageConverter().toMessage(object, new MessageProperties());
    }

獲取消息轉(zhuǎn)換器的代碼如下:

private MessageConverter getRequiredMessageConverter() throws IllegalStateException {
        MessageConverter converter = getMessageConverter();
        if (converter == null) {
            throw new AmqpIllegalStateException(
                    "No 'messageConverter' specified. Check configuration of RabbitTemplate.");
        }
        return converter;
    }

getMessageConverter就是獲取rabbitmqTemplate 類中的一個field。

public MessageConverter getMessageConverter() {
        return this.messageConverter;
    }
我們只要看哪里對它進行賦值即可。

然后我想起來,就是在我們業(yè)務(wù)代碼里賦值的:

@Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        // 下面這里賦值了。。。差點搞忘了
        template.setMessageConverter(new Jackson2JsonMessageConverter());
        template.setMandatory(true);

        return template;
    }

反正呢,總體來說,就是rabbitmqTemplate 會使用我們自定義的messageConverter轉(zhuǎn)換message后再發(fā)送。

時區(qū)問題,很好重現(xiàn),源碼在:

https://gitee.com/ckl111/all-simple-demo-in-work/tree/master/jackson-demo

@Data
public class TestVO {
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date testDate;
}

測試代碼:

@org.junit.Test
    public void normal() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        TestVO vo = new TestVO();
        vo.setTestDate(new Date());
        String value = mapper.writeValueAsString(vo);
        System.out.println(value);
    }

輸出:

{"testDate":"2019-12-27 05:45:26"}

解決辦法

指定默認(rèn)時區(qū)配置

@org.junit.Test
    public void specifyDefaultTimezone() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        SerializationConfig oldSerializationConfig = mapper.getSerializationConfig();
        /**
         * 新的序列化配置,要配置時區(qū)
         */
        String timeZone = "GMT+8";
        SerializationConfig newSerializationConfig = oldSerializationConfig.with(TimeZone.getTimeZone(timeZone));

        mapper.setConfig(newSerializationConfig);
        TestVO vo = new TestVO();
        vo.setTestDate(new Date());
        String value = mapper.writeValueAsString(vo);
        System.out.println(value);
    }

在field上加注解

@Data
public class TestVoWithTimeZone {
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date testDate;
}
我們這里,新增了timezone,手動指定了時區(qū)配置。

測試代碼:

@org.junit.Test
    public void specifyTimezoneOnField() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        TestVoWithTimeZone vo = new TestVoWithTimeZone();
        vo.setTestDate(new Date());
        String value = mapper.writeValueAsString(vo);
        System.out.println(value);
    }

上面兩種的輸出都是正確的。

這里沒有去分析源碼,簡單說一下,在序列化的時候,會有一個序列化配置;這個配置由兩部分組成:默認(rèn)配置+這個類自定義的配置。 自定義配置會覆蓋默認(rèn)配置。

我們的第二種方式,就是修改了默認(rèn)配置;第三種方式,就是使用自定義配置覆蓋默認(rèn)配置。

jackson 還挺重要,尤其是 spring cloud 全家桶, feign 也用了這個, restTemplate 也用了,還有 Spring MVC 里的 httpmessageConverter 有興趣的同學(xué),去看下面這個地方就可以了。
rabbitmq template實現(xiàn)發(fā)送消息

rabbitmq template實現(xiàn)發(fā)送消息

如果對JsonFormat的處理感興趣,可以看下面的地方:

com.fasterxml.jackson.annotation.JsonFormat.Value#Value(com.fasterxml.jackson.annotation.JsonFormat) (打個斷點在這里,然后跑個test就到這里了)
rabbitmq template實現(xiàn)發(fā)送消息

rabbitmq template實現(xiàn)發(fā)送消息

總結(jié)
差點忘了,針對rabbitmq template的問題,最終我們的解決方案就是:

@Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);

        ObjectMapper mapper = new ObjectMapper();
        SerializationConfig oldSerializationConfig = mapper.getSerializationConfig();
        /**
         * 新的序列化配置,要配置時區(qū)
         */
        String timeZone = environment.getProperty(CadModuleConstants.SPRING_JACKSON_TIME_ZONE);
        SerializationConfig newSerializationConfig = oldSerializationConfig.with(TimeZone.getTimeZone(timeZone));

        mapper.setConfig(newSerializationConfig);

        Jackson2JsonMessageConverter messageConverter = new Jackson2JsonMessageConverter(mapper);

        template.setMessageConverter(messageConverter);
        template.setMandatory(true);

        ...設(shè)置callback啥的
        return template;
    }

網(wǎng)頁題目:rabbitmqtemplate實現(xiàn)發(fā)送消息
鏈接地址:http://aaarwkj.com/article16/iposdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、全網(wǎng)營銷推廣、網(wǎng)站改版、營銷型網(wǎng)站建設(shè)網(wǎng)站設(shè)計、關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
亚洲精品一区二区毛豆| 青青草日韩视频在线观看| 国产91在线精品超碰人人| 全黄性性激高免费放视频| 国产精精精精品欧美日韩| 人人妻人人澡人人爽人人dvd| 亚洲熟女熟妇另类中文| 国产不卡高清视频在线| 偷拍偷窥女厕一区二区视频| 91九色视频免费观看| 人人妻人人澡人人爽人人dvd| 精品一区二区三区女同| 亚洲第六页亚洲第一页| 国产精品传媒在线视频| 亚洲免费精品一区二区三区四区| 亚洲免费一区二区三区精品| 成人在线免费黄色小说| 91欧美精品综合在线| 日韩欧美在线一区二区| 日韩黄国产一区二区三| 蜜臀av免费在线观看| 日韩看片一区二区三区高清| 日韩欧美一级性生活片| 欧美日韩亚洲中文国产| 欧美一级特黄大片免色| 国产成人性生交大片免费| 国产一区二区三区精品女同| 深夜av一区二区三区| 亚洲三级黄片免费播放| 亚洲欧美日韩国产桃色| 91欧美日韩精品在线| 中午字幕久久亚洲精品| 国产亚洲欧美日韩各类| 在线欧美亚洲观看天堂| 亚洲黄香蕉视频免费看| 黑丝美女大战白丝美女| 欧美精品日韩精品一区二区| 中文字幕乱码av一区二区 | 国产精品久久久99| 久久香蕉香蕉公开视频| 久久精品一偷一偷国产|