com.jms.notify.message.ConsumerSessionAwareMessageListener.java Source code

Java tutorial

Introduction

Here is the source code for com.jms.notify.message.ConsumerSessionAwareMessageListener.java

Source

/*
 * Copyright 2015-2102 RonCoo(http://www.roncoo.com) Group.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jms.notify.message;

import java.util.Date;

import javax.jms.Message;
import javax.jms.MessageListener;

import org.apache.activemq.command.ActiveMQTextMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jms.notify.core.NotifyPersist;
import com.jms.notify.core.NotifyQueue;
import com.jms.notify.entity.RpNotifyRecord;
import com.jms.notify.enums.NotifyStatusEnum;
import com.jms.notify.exception.BizException;
import com.jms.notify.service.RpNotifyService;
import com.jms.notify.utils.StringUtil;

/**
 * 
* @ClassName: ConsumerSessionAwareMessageListener
* @Description: TODO(????)
* @author :Administrator
* @uauthor Administrator TODO(???????)
* @cdate :20161020
* @udate :20161020  TODO(????)
* @version V1.0 
*
 */
public class ConsumerSessionAwareMessageListener implements MessageListener {

    private static final Log log = LogFactory.getLog(ConsumerSessionAwareMessageListener.class);

    @Autowired
    private NotifyQueue notifyQueue;

    @Autowired
    private RpNotifyService rpNotifyService;

    @Autowired
    private NotifyPersist notifyPersist;

    @SuppressWarnings("static-access")
    public void onMessage(Message message) {
        try {
            ActiveMQTextMessage msg = (ActiveMQTextMessage) message;
            final String ms = msg.getText();
            log.info("== receive message:" + ms);

            JSON json = (JSON) JSONObject.parse(ms);
            RpNotifyRecord notifyRecord = JSONObject.toJavaObject(json, RpNotifyRecord.class);
            if (notifyRecord == null) {
                return;
            }
            // log.info("notifyParam:" + notifyParam);
            notifyRecord.setStatus(NotifyStatusEnum.CREATED.name());
            notifyRecord.setCreateTime(new Date());
            notifyRecord.setLastNotifyTime(new Date());

            if (!StringUtil.isEmpty(notifyRecord.getId())) {
                RpNotifyRecord notifyRecordById = rpNotifyService.getNotifyRecordById(notifyRecord.getId());
                if (notifyRecordById != null) {
                    return;
                }
            }

            while (rpNotifyService == null) {
                Thread.currentThread().sleep(1000); // Spring ?????
            }

            try {
                // ???
                notifyPersist.saveNotifyRecord(notifyRecord);
                notifyRecord = rpNotifyService.getNotifyByMerchantNoAndMerchantOrderNoAndNotifyType(
                        notifyRecord.getMerchantNo(), notifyRecord.getMerchantOrderNo(),
                        notifyRecord.getNotifyType());

                // 
                notifyQueue.addElementToList(notifyRecord);
            } catch (BizException e) {
                log.error("BizException :", e);
            } catch (Exception e) {
                log.error(e);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e);
        }
    }

}