admin
2022-09-30 ffc56f912da5d6d842142ae4ea1856bc56f8bcb9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.yeshi.makemoney.app;
 
import com.google.gson.Gson;
import com.ks.lib.common.exception.ParamsException;
import com.yeshi.makemoney.app.dto.mq.ExtractTransferResultMQMsg;
import com.yeshi.makemoney.app.dto.mq.GoldCornSettleMQMsg;
import com.yeshi.makemoney.app.exception.goldcorn.GoldCornConsumeRecordException;
import com.yeshi.makemoney.app.exception.goldcorn.GoldCornMoneyExchangeRateRecordException;
import com.yeshi.makemoney.app.exception.money.UserMoneyRecordException;
import com.yeshi.makemoney.app.exception.user.UserInfoException;
import com.yeshi.makemoney.app.service.inter.goldcorn.GoldCornSettleService;
import com.yeshi.makemoney.app.service.inter.money.ExtractService;
import com.yeshi.makemoney.app.utils.Constant;
import com.yeshi.makemoney.app.utils.LogUtil;
import com.yeshi.makemoney.app.utils.mq.CMQManager;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.yeshi.utils.mq.JobThreadExecutorServiceImpl;
import org.yeshi.utils.tencentcloud.COSManager;
import org.yeshi.utils.tencentcloud.entity.COSInitParams;
 
import javax.annotation.Resource;
import javax.servlet.MultipartConfigElement;
import java.io.File;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
 
//不引入数据库
//@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class,DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class
//        , DruidDataSourceAutoConfigure.class, HibernateJpaAutoConfiguration.class})
@MapperScan(basePackages = "com.yeshi.makemoney.app.dao")
@SpringBootApplication
@EnableTransactionManagement
//@EnableDubbo(scanBasePackages = "com.ks.push.service.remote")
public class Application implements ApplicationListener<ContextRefreshedEvent> {
    Logger settleLogger= LoggerFactory.getLogger("settleLog");
    private final static Logger logger = LoggerFactory.getLogger(Application.class);
 
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
        logger.info(Constant.wxTransferConfig.toString());
        logger.info("应用启动成功");
    }
 
 
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        logger.info("容器加载完毕");
        initMQMsgConsumer();
        initCOS();
        LogUtil.settleLogger.info("test123");
 
    }
 
    @Resource
    private GoldCornSettleService goldCornSettleService;
 
    @Resource
    private ExtractService extractService;
 
    private void initMQMsgConsumer() {
        final int THREAD_NUM = 3;
 
        //创建三条队列处理
        for (int i = 0; i < THREAD_NUM; i++) {
            new JobThreadExecutorServiceImpl().run(new Runnable() {
                @Override
                public void run() {
 
                    Map<String, GoldCornSettleMQMsg> map = CMQManager.getInstance().consumeGoldCornSettleMsg(10);
                    if (map != null) {
                        for (Iterator<String> its = map.keySet().iterator(); its.hasNext(); ) {
                            String key = its.next();
                            GoldCornSettleMQMsg msg = map.get(key);
                            try {
                                settleLogger.info("消费结算消息开始:{}",new Gson().toJson(msg));
                                goldCornSettleService.settle(msg.getUid(), msg.getDay(),msg.getSettleId());
                                CMQManager.getInstance().deleteGoldCornSettleMsg(key);
                                settleLogger.info("消费结算消息成功:{}",new Gson().toJson(msg));
                            } catch (UserInfoException e) {
                                e.printStackTrace();
                                settleLogger.error("消费结算消息异常:{} ",new Gson().toJson(msg),e);
                            } catch (GoldCornMoneyExchangeRateRecordException e) {
                                e.printStackTrace();
                                settleLogger.error("消费结算消息异常:{} ",new Gson().toJson(msg),e);
                            } catch (UserMoneyRecordException e) {
                                e.printStackTrace();
                                settleLogger.error("消费结算消息异常:{} ",new Gson().toJson(msg),e);
                            } catch (GoldCornConsumeRecordException e) {
                                e.printStackTrace();
                                settleLogger.error("消费结算消息异常:{} ",new Gson().toJson(msg),e);
                            } catch (Exception e) {
                                e.printStackTrace();
                                settleLogger.error("消费结算消息异常:{} ",new Gson().toJson(msg),e);
                            }
                        }
                    }
 
 
                }
            });
        }
 
        //处理提现结果
        new JobThreadExecutorServiceImpl().run(new Runnable() {
            @Override
            public void run() {
                Map<String, ExtractTransferResultMQMsg> map = CMQManager.getInstance().consumeExtractResultMsg(1);
                if (map != null) {
                    for (Iterator<String> its = map.keySet().iterator(); its.hasNext(); ) {
                        String key = its.next();
                        ExtractTransferResultMQMsg msg = map.get(key);
                        try {
                            extractService.processExtractResult(msg);
                            CMQManager.getInstance().deleteExtractResultMsg(key);
                        } catch (ParamsException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
 
    }
 
 
    /**
     * 对象存储初始化
     */
    public static void initCOS() {
        // 载入cos参数
        Properties ps = org.yeshi.utils.PropertiesUtil
                .getProperties(Application.class.getClassLoader().getResourceAsStream("cos.properties"));
        COSInitParams params = new COSInitParams();
        params.setAppId(Long.parseLong(ps.getProperty("appId")));
        params.setBucketName(ps.getProperty("bucketName"));
        params.setRegion(ps.getProperty("region"));
        params.setSecretId(ps.getProperty("secretId"));
        params.setSecretKey(ps.getProperty("secretKey"));
        // 初始化
        COSManager.getInstance().init(params);
    }
 
    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        String location = System.getProperty("user.dir") + "/data/tmp";
        File tmpFile = new File(location);
        if (!tmpFile.exists()) {
            tmpFile.mkdirs();
        }
        factory.setLocation(location);
        return factory.createMultipartConfig();
    }
 
}