From 4b7b53dd6439f30331bdfbe973fc5883c649d352 Mon Sep 17 00:00:00 2001 From: welsir <1824379011@qq.com> Date: Mon, 27 May 2024 23:09:09 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=A6=E5=8F=B7=E6=A8=A1=E5=9D=97=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=91=BD=E5=90=8D=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/org/example/api/VideoPushApi.java | 2 +- .../java/org/example/core/exchange/Exchange.java | 3 ++- .../example/core/exchange/PostWorkerManager.java | 11 ++++------- .../DefaultVideoPushStrategy.java | 3 +-- .../factory/videoPushFactory/StrategyFactory.java | 2 +- .../videoPushFactory/VideoPushStrategy.java | 2 +- .../example/core/guard/VideoPushChannelGuard.java | 15 +++++++++++---- 7 files changed, 21 insertions(+), 17 deletions(-) diff --git a/chopperbot-account/src/main/java/org/example/api/VideoPushApi.java b/chopperbot-account/src/main/java/org/example/api/VideoPushApi.java index e37a05e..f7ab6cc 100644 --- a/chopperbot-account/src/main/java/org/example/api/VideoPushApi.java +++ b/chopperbot-account/src/main/java/org/example/api/VideoPushApi.java @@ -14,7 +14,7 @@ import javax.annotation.Resource; * @Date 2023/9/5 22:36 */ @RestController -@RequestMapping("") +@RequestMapping("/video") public class VideoPushApi { @Resource diff --git a/chopperbot-account/src/main/java/org/example/core/exchange/Exchange.java b/chopperbot-account/src/main/java/org/example/core/exchange/Exchange.java index 1e32e52..ee0cb0c 100644 --- a/chopperbot-account/src/main/java/org/example/core/exchange/Exchange.java +++ b/chopperbot-account/src/main/java/org/example/core/exchange/Exchange.java @@ -44,7 +44,7 @@ public class Exchange { //切片推送 public void work(){ - System.out.println("执行视频发布推送工作.........."); + log.info("执行视频发布推送工作.........."); Map> channelAccount = channel.getChannelAccount(); channels.forEach((k,v)->{ if(channels.get(k)==null||channelAccount.get(k)==null){ @@ -54,6 +54,7 @@ public class Exchange { List packageSections = channels.get(k); for (Account account : accountList) { for (PackageSection packageSection : packageSections) { + //换成builder VideoToPublish video = new VideoToPublish(); video.setCookies(account.getCookies()); video.setTitle(packageSection.getTitle()); diff --git a/chopperbot-account/src/main/java/org/example/core/exchange/PostWorkerManager.java b/chopperbot-account/src/main/java/org/example/core/exchange/PostWorkerManager.java index db80429..994c2cd 100644 --- a/chopperbot-account/src/main/java/org/example/core/exchange/PostWorkerManager.java +++ b/chopperbot-account/src/main/java/org/example/core/exchange/PostWorkerManager.java @@ -27,10 +27,6 @@ import java.util.concurrent.TimeUnit; @Component public class PostWorkerManager extends SpringBootPlugin { - int coreSize = 15; - int maxSize = 20; - ThreadPoolExecutor executor; - HashMap temporaryPool = new HashMap<>(); @Resource Exchange exchange; @@ -40,7 +36,6 @@ public class PostWorkerManager extends SpringBootPlugin { VideoTemporaryMapper mapper; @Override public boolean init() { - executor = new ThreadPoolExecutor(coreSize,maxSize,0, TimeUnit.SECONDS,new LinkedBlockingQueue<>(),new ThreadPoolExecutor.AbortPolicy()); startProcess(); return super.init(); } @@ -60,15 +55,17 @@ public class PostWorkerManager extends SpringBootPlugin { for (PacketSectionVideo video : videoList) { String route = String.join(".", video.getLabels()); //是否自动推送 - if(video.isRelay()){ + if(!video.isAuto()){ temporaryPool.put(video.getId(),video); continue; } exchange.pushToQueue(route,video); video.setFinish(true); - videoStorehouse.setCount(); + videoStorehouse.decrementCount(); } } + //如何保证数据一定会被消费且不会重复消费 + exchange.work(); saveToLocal(); } diff --git a/chopperbot-account/src/main/java/org/example/core/factory/videoPushFactory/DefaultVideoPushStrategy.java b/chopperbot-account/src/main/java/org/example/core/factory/videoPushFactory/DefaultVideoPushStrategy.java index dce7ba2..40196fd 100644 --- a/chopperbot-account/src/main/java/org/example/core/factory/videoPushFactory/DefaultVideoPushStrategy.java +++ b/chopperbot-account/src/main/java/org/example/core/factory/videoPushFactory/DefaultVideoPushStrategy.java @@ -18,9 +18,8 @@ public class DefaultVideoPushStrategy extends StrategyFactory { private List priorityImportWord = new ArrayList<>(); - @Override - public PacketSectionVideo rule(PackageSection obj) { + public PacketSectionVideo wrapperSectionVideo(PackageSection obj) { if(obj != null){ return wrapPacketSection(obj); } diff --git a/chopperbot-account/src/main/java/org/example/core/factory/videoPushFactory/StrategyFactory.java b/chopperbot-account/src/main/java/org/example/core/factory/videoPushFactory/StrategyFactory.java index 12c4813..f386923 100644 --- a/chopperbot-account/src/main/java/org/example/core/factory/videoPushFactory/StrategyFactory.java +++ b/chopperbot-account/src/main/java/org/example/core/factory/videoPushFactory/StrategyFactory.java @@ -7,7 +7,7 @@ package org.example.core.factory.videoPushFactory; */ public abstract class StrategyFactory implements VideoPushStrategy { - public static StrategyFactory connect(int x){ + public static StrategyFactory selectStrategy(int x){ switch (x) { case 0: return new DefaultVideoPushStrategy(); diff --git a/chopperbot-account/src/main/java/org/example/core/factory/videoPushFactory/VideoPushStrategy.java b/chopperbot-account/src/main/java/org/example/core/factory/videoPushFactory/VideoPushStrategy.java index c6da3ae..defad3a 100644 --- a/chopperbot-account/src/main/java/org/example/core/factory/videoPushFactory/VideoPushStrategy.java +++ b/chopperbot-account/src/main/java/org/example/core/factory/videoPushFactory/VideoPushStrategy.java @@ -12,7 +12,7 @@ import java.util.List; */ public interface VideoPushStrategy { - PacketSectionVideo rule(PackageSection obj); + PacketSectionVideo wrapperSectionVideo(PackageSection obj); List queryPriority(); diff --git a/chopperbot-account/src/main/java/org/example/core/guard/VideoPushChannelGuard.java b/chopperbot-account/src/main/java/org/example/core/guard/VideoPushChannelGuard.java index 1e632aa..91c63be 100644 --- a/chopperbot-account/src/main/java/org/example/core/guard/VideoPushChannelGuard.java +++ b/chopperbot-account/src/main/java/org/example/core/guard/VideoPushChannelGuard.java @@ -5,8 +5,10 @@ import org.example.bean.section.PackageSection; import org.example.core.factory.videoPushFactory.StrategyFactory; import org.example.plugin.SpringGuardPlugin; import org.example.pojo.PacketSectionVideo; +import org.example.sql.SQLInitHelper; import org.springframework.stereotype.Component; +import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ArrayBlockingQueue; @@ -29,9 +31,12 @@ public class VideoPushChannelGuard extends SpringGuardPlugin { private int val = 0; private AtomicInteger count = new AtomicInteger(0); + @Resource + SQLInitHelper sqlInitHelper; @Override public boolean init() { - factory = StrategyFactory.connect(val); + factory = StrategyFactory.selectStrategy(val); + sqlInitHelper.initTable("videoTemp",""); return true; } @@ -41,7 +46,8 @@ public class VideoPushChannelGuard extends SpringGuardPlugin { if(p==null){ return; } - PacketSectionVideo packedVideo = factory.rule(p); + //数据库持久化保存 + PacketSectionVideo packedVideo = factory.wrapperSectionVideo(p); videosCollection.put(packedVideo.getId(),packedVideo); count.incrementAndGet(); } @@ -51,10 +57,10 @@ public class VideoPushChannelGuard extends SpringGuardPlugin { } public boolean pushNotify(){ - return count.get()!=0; + return count.get()!=0; } - public void setCount(){ + public void decrementCount(){ count.decrementAndGet(); } public List priority(){ @@ -72,4 +78,5 @@ public class VideoPushChannelGuard extends SpringGuardPlugin { .forEach(list::add); return list; } + }