续接上文
本文我们介绍 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);
}
}
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏