SVN可视化平台(5) -- RabbitMQ监听消息,创建文件

avatar 2021年12月20日11:41:56 6 761 views
博主分享免费Java教学视频,B站账号:Java刘哥

续接上文

本文我们介绍 RabbitMQ监听 svn hooks 提交的消息

关于 hooks 提交消息的,具体可以看这篇文章:SVN hook post-commit 设置自动更新仓库代码和发送请求

直接上代码

代码主要逻辑是获取提交的仓库地址和版本号,然后去查日志信息,然后查询需要添加/删除的文件列表,并进行操作

直接上代码

1、依赖

<!-- rabbitMQ -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<version>2.3.4.RELEASE</version>

2、配置

spring:
# RabbitMQ 配置
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: 账号
    password: 密码
    virtual-host: svn_vhost

3、消费者代码

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * SVN文件提交消费者
 *
 * @author liuyanzhao
 */
@Slf4j
@Component
@RefreshScope
public class SVNFileCommitConsumerDemo
{


    @Autowired
    private SvnSubmitLogService submitLogService;

    @Autowired
    private CommonFileService commonFileService;

    public static final String repoUrl = "http://127.0.0.1/svn/项目";


    /**
     * 消息监听
     * 自动创建,Exchange和Queue绑定
     *
     * @param message
     * @throws UnsupportedEncodingException
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "svn_queue"),
            exchange = @Exchange(value = "svn_exchange", type = "x-delayed-message")
    ))
    public void process(String message) throws UnsupportedEncodingException, InterruptedException
    {
        log.info("SVNFileCommitConsumer message:{}", message);
        JSONObject jsonObject = null;
        try
        {
            jsonObject = JSONObject.parseObject(message.replace("\\", "/"));
        } catch (Exception e)
        {
            log.error("SVNFileCommitConsumer json不合法:{}", message);
            return;
        }
        String version = jsonObject.getString("ver");
        String repoPath = jsonObject.getString("repo");
        if (CommonUtil.isEmpty(version) || CommonUtil.isEmpty(repoPath))
        {
            return;
        }

        // 获得日志信息
        SVNCommitLogDTO commitLog = SVNUtil.getCommitLog("admin", "123456", repoUrl, Long.valueOf(version));
        if (commitLog == null || CommonUtil.isEmpty(commitLog.getFileList()))
        {
            return;
        }
        commitLog.setRepoPrefix(repoUrl);
        // 获得文件列表
        List<SVNFileEntryDTO> fileList = SVNUtil.getFileInfo("admin", "123456", repoUrl, commitLog.getFileList());
        // 需要删除的文件列表
        List<SVNFileEntryDTO> needDeleteFileList = fileList.stream().filter(p -> "D".equals(p.getSubmitType())).collect(Collectors.toList());
        // 需要添加/更新的文件列表
        List<SVNFileEntryDTO> needSaveFileList = fileList.stream().filter(p -> !"D".equals(p.getSubmitType())).collect(Collectors.toList());

        // 处理添加或更新文件
        commonFileService.insertOrUpdate(needSaveFileList, commitLog);

        // 处理删除文件
        needDeleteFileList = needDeleteFileList.stream().filter(p -> p != null).collect(Collectors.toList());
        commonFileService.delete(needDeleteFileList, commitLog);
    }


}
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
可以匿名评论或者登录后台评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0