MiraiForum

    • Register
    • Login
    • Search
    • Popular
    • Recent
    • Unsolved
    • Tags
    • Groups
    • 友情链接

    Java + Springboot 2.X+ mirai最新RELEASE の采坑记录

    精华主题
    spring boot mirai core java
    7
    26
    6203
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • L
      liuzheng2000 last edited by

      大佬,目前整合的项目开源了吗?想去学习下?

      wssy001 1 Reply Last reply Reply Quote 0
      • wssy001
        wssy001 ⭐2021⭐ @liuzheng2000 last edited by

        @liuzheng2000 个人觉得对springboot熟悉的人看我写的示例应该知道怎么整合mirai core。如有疑问可以发到这儿或者论坛其他地方。

        L 1 Reply Last reply Reply Quote 0
        • L
          liuzheng2000 @wssy001 last edited by

          @wssy001 嗯,我自己也写了点。主要是也是想看看不同得人,不同得写法。

          1 Reply Last reply Reply Quote 0
          • wssy001
            wssy001 ⭐2021⭐ last edited by wssy001

            新的一年新气象、祝大家:虎年虎虎生威
            mirai也发布了2.10.0版本,不废话,给出GAV坐标:

            <dependency>
                <groupId>net.mamoe</groupId>
                <artifactId>mirai-core-jvm</artifactId>
                <version>${mirai.version}</version>
            </dependency>
            

            因为mirai 2.10.0版本将kotlin所需版本拉到了1.6.0,所以还需要手动提升相关依赖的版本

            <properties>
                <kotlin.version>1.6.0</kotlin.version>
                <kotlin-coroutines.version>1.6.0</kotlin-coroutines.version>
                <mirai.version>2.10.0</mirai.version>
            </properties>
            
            1 Reply Last reply Reply Quote 0
            • wssy001
              wssy001 ⭐2021⭐ last edited by wssy001

              最近在进行code review,发现我一处值得优化的代码块
              大致的逻辑是:使用@PostConstruct标注一个init()方法,让其完成bot登录,顺便进行一些数据缓存。
              大致的代码如下↓↓↓

              @PostConstruct
              public void init() {
                  Bot bot=……  //忽略Bot的构造
                  login(bot);
              
              //  数据缓存
                  cacheWhiteList();
                  cacheAdminList();
              }
              

              存在的问题:堵塞main线程。

              由于数据缓存等方法都在一个方法体内、各自相互独立但又必须在login()方法执行后,可以多线程。同时查阅SpringBootApplication的生命周期,找出一个执行顺序在@PostConstruct后运行即可保证bot完成登录后再执行数据缓存。

              问题缓解:
              将这些与login()不相干的数据缓存方法提取成一个service、监听ApplicationStartedEvent,当SpringBootApplication启动后会发布一个相关的Event,监听后再执行具体的数据缓存方法
              大致的代码:

              @Slf4j
              @Service
              public class CollectInfoService implements ApplicationListener<ApplicationStartedEvent> {
              
                  @Override
                  public void onApplicationEvent(ApplicationStartedEvent event) {
                      Thread cacheWhiteList = new Thread(this::cacheWhiteList);
                      cacheWhiteList.setName("CacheWhiteList");
                      cacheWhiteList.start();
              
                      Thread cacheAdminList = new Thread(this::cacheAdminList);
                      cacheAdminList.setName("CacheAdminList");
                      cacheAdminList.start();
                  }
              
                  public void cacheWhiteList() {
                  }
              
                  public void cacheAdminList() {
                  }
              }
              
              1 Reply Last reply Reply Quote 0
              • Q
                qycss last edited by

                牛!

                1 Reply Last reply Reply Quote 0
                • 1
                • 2
                • 2 / 2
                • First post
                  Last post
                Powered by Mamoe Technologies & NodeBB | 友情链接 | 服务监控 | Contact