Merge branch 'master' of D:\ideaProject\ad-platform with conflicts.

master
wujingtao 3 years ago
parent f367d6866f
commit 1271aeb0e9

@ -0,0 +1,37 @@
package com.baiye.model.entity;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.util.Date;
/**
* @author wujingtao
* @date 2022/01/04
*/
@Getter
@Setter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class BaseTimeTask {
@Column(name = "execute_time")
private Date executeTime;
@Column(name = "is_repeat")
private Integer isRepeat;
@Column(name = "status")
private Integer status;
@Column(name = "message")
private String message;
@Column(name = "user_id")
private Long userId;
}

@ -15,6 +15,9 @@ spring:
url: jdbc:mysql://118.178.137.129:3306/ad-platform?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&rewriteBatchedStatements=true&zeroDateTimeBehavior=convertToNull url: jdbc:mysql://118.178.137.129:3306/ad-platform?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&rewriteBatchedStatements=true&zeroDateTimeBehavior=convertToNull
username: root username: root
password: root password: root
# url: jdbc:mysql://localhost:3306/ad-platform?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false&zeroDateTimeBehavior=convertToNull
# username: root
# password: 12345678
# 初始连接数 # 初始连接数
initial-size: 5 initial-size: 5
# 最小连接数 # 最小连接数
@ -124,3 +127,6 @@ file:
# 文件大小 /M # 文件大小 /M
maxSize: 100 maxSize: 100
avatarMaxSize: 5 avatarMaxSize: 5
jpa:
show-sql: true

@ -6,6 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableAsync;
/** /**
* 广 * 广
@ -18,6 +19,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@EnableFeignClients @EnableFeignClients
@EnableDiscoveryClient @EnableDiscoveryClient
@SpringBootApplication @SpringBootApplication
@EnableAsync
public class AdPlatformTaskApplication { public class AdPlatformTaskApplication {
public static void main(String[] args) { public static void main(String[] args) {

@ -0,0 +1,23 @@
package com.baiye.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import java.util.concurrent.*;
/**
* @author wujingtao
* @date 2022/01/04
*/
@Configuration
@EnableAsync
public class ThreadPoolConfig {
@Bean(value = "taskExecutor")
public Executor taskExecutor() {
return new ThreadPoolExecutor(
2, 16, 3, TimeUnit.SECONDS, new LinkedBlockingDeque<>(3),
Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardOldestPolicy());
}
}

@ -0,0 +1,34 @@
package com.baiye.job;
import com.baiye.modules.elsaticjob.entity.jobInstance.ElasticSimpleJob;
import com.baiye.modules.elsaticjob.service.impl.AutoReminderServiceImpl;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
/**
* @author wujingtao
* @date 2022/01/05
*/
@Slf4j
@Component
@ElasticSimpleJob(jobName = "AutomaticReminderJob", cron = "0 0 0/1 * * ?")
public class AutomaticReminderJob implements SimpleJob {
@Resource
private AutoReminderServiceImpl timeTaskServiceImpl;
private static AutomaticReminderJob automaticReminderJob;
@PostConstruct
public void init() {
automaticReminderJob = this;
}
@Override
public void execute(ShardingContext shardingContext) {
automaticReminderJob.timeTaskServiceImpl.runAutomaticReminder();
}
}

@ -0,0 +1,36 @@
package com.baiye.modules.elsaticjob.api;
import com.baiye.http.CommonResponse;
import com.baiye.modules.elsaticjob.entity.AutoReminder;
import com.baiye.modules.elsaticjob.service.AutoReminderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* @author wujingtao
* @date 2022/01/05
*/
@Slf4j
@RestController
@RequestMapping("/api/time")
@Api(tags = "操作定时任务的条件")
public class AutoReminderController {
@Resource
private AutoReminderService timeTaskService;
/**
*
*
* @param timeTask
*/
@PostMapping("/add")
@ApiOperation("新增定时任务")
public CommonResponse<Object> addTimeTask(@RequestBody AutoReminder timeTask) {
return timeTaskService.addTimeTask(timeTask);
}
}

@ -0,0 +1,35 @@
package com.baiye.modules.elsaticjob.dao;
import com.baiye.modules.elsaticjob.entity.AutoReminder;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author wujingtao
* @date 2022/01/05
*/
@Repository
public interface AutoReminderRepository extends JpaRepository<AutoReminder, Long>, JpaSpecificationExecutor<AutoReminder> {
/**
*
*
* @param status
* @return
*/
List<AutoReminder> findAllByStatus(Integer status);
/**
*
*
* @param status
* @param id
*/
@Modifying
@Query(value = "update AutoReminder set status =?1 where id =?2")
void updateTimeTaskStatus(Integer status, Long id);
}

@ -0,0 +1,36 @@
package com.baiye.modules.elsaticjob.entity;
import cn.hutool.core.date.DatePattern;
import com.baiye.model.entity.BaseTimeTask;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
/**
* @author wujingtao
* @date 2022/01/05
*/
@Getter
@Setter
@Entity
@Table(name = "tb_auto_reminder")
@EntityListeners(AuditingEntityListener.class)
public class AutoReminder extends BaseTimeTask implements Serializable {
private static final long serialVersionUID = 1343708488273542805L;
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@LastModifiedDate
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = DatePattern.NORM_DATETIME_PATTERN, timezone = "GMT+8")
@Column(name = "create_time")
private Date createTime;
}

@ -0,0 +1,18 @@
package com.baiye.modules.elsaticjob.service;
import com.baiye.http.CommonResponse;
import com.baiye.modules.elsaticjob.entity.AutoReminder;
/**
* @author wujingtao
* @date 2022/01/05
*/
public interface AutoReminderService {
/**
*
*
* @param timeTask
* @return
*/
CommonResponse<Object> addTimeTask(AutoReminder timeTask);
}

@ -0,0 +1,68 @@
package com.baiye.modules.elsaticjob.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baiye.constant.DefaultNumberConstants;
import com.baiye.http.CommonResponse;
import com.baiye.modules.elsaticjob.dao.AutoReminderRepository;
import com.baiye.modules.elsaticjob.entity.AutoReminder;
import com.baiye.modules.elsaticjob.service.AutoReminderService;
import com.baiye.socket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
/**
* @author wujingtao
* @date 2022/01/05
*/
@Slf4j
@Service
public class AutoReminderServiceImpl implements AutoReminderService {
@Resource
private AutoReminderRepository timeTaskRepository;
@Resource
private WebSocketServer webSocketServer;
@Override
public CommonResponse<Object> addTimeTask(AutoReminder timeTask) {
if (timeTaskRepository.save(timeTask).getId() == null) {
return CommonResponse.createByErrorMessage("保存定时任务失败");
}
return CommonResponse.createBySuccess();
}
/**
*
*/
@Transactional(rollbackFor = Exception.class)
public void runAutomaticReminder() {
List<AutoReminder> allByStatus = timeTaskRepository.findAllByStatus(DefaultNumberConstants.ZERO_NUMBER);
if (CollUtil.isEmpty(allByStatus)) {
return;
}
JSONObject object = new JSONObject();
object.putOpt("type", "");
object.putOpt("code", DefaultNumberConstants.TWO_HUNDRED);
for (AutoReminder info : allByStatus) {
if (DateUtil.between(info.getExecuteTime(), DateUtil.date(), DateUnit.HOUR) == 0) {
object.putOpt("message", info.getUserId());
try {
webSocketServer.sendMessage(JSONUtil.toJsonStr(object), String.valueOf(info.getUserId()));
} catch (Exception e) {
log.info("执行自提醒任务 id:{} 发送websocket失败 {}", info.getId(), e.getMessage());
continue;
}
if (info.getIsRepeat() == DefaultNumberConstants.ZERO_NUMBER) {
timeTaskRepository.updateTimeTaskStatus(DefaultNumberConstants.ONE_NUMBER, info.getId());
}
}
}
}
}

@ -138,7 +138,8 @@ public class WebSocketServer {
* @param sessionId * @param sessionId
* @throws IOException * @throws IOException
*/ */
public static void sendMessage(String message, String sessionId) throws IOException { public void sendMessage(String message, String sessionId) throws IOException {
log.info("发送web信息 {}", message);
Session session = null; Session session = null;
if (SESSIONS.get(sessionId) != null) { if (SESSIONS.get(sessionId) != null) {
session = SESSIONS.get(sessionId); session = SESSIONS.get(sessionId);
@ -152,8 +153,6 @@ public class WebSocketServer {
public void sendMessage(ReportMessageInfoVO reportMessageInfoVO) throws IOException { public void sendMessage(ReportMessageInfoVO reportMessageInfoVO) throws IOException {
log.info("发给webSocket信息 {}", reportMessageInfoVO);
Session session = null; Session session = null;
for (String key : SESSIONS.keySet()) { for (String key : SESSIONS.keySet()) {
if (key.equals(String.valueOf(reportMessageInfoVO.getUserId()))) { if (key.equals(String.valueOf(reportMessageInfoVO.getUserId()))) {

@ -174,6 +174,8 @@
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
@ -190,6 +192,7 @@
<artifactId>guava</artifactId> <artifactId>guava</artifactId>
<version>${guava.version}</version> <version>${guava.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<finalName>${project.name}</finalName> <finalName>${project.name}</finalName>

@ -32,12 +32,12 @@ public class SettingTest {
@Test @Test
public void getSetting() { public void getSetting() {
DistributeDTO distributeDTO = new DistributeDTO(); DistributeDTO distributeDTO = new DistributeDTO();
List<Long> strings = Lists.newArrayList(1L,2L,3L,4L,5L,6L,7L,8L,9L,10L);
List<Long> longs = Lists.newArrayList(12360L, 12361L,12362L,12363L,12365L); List<String> strings = Lists.newArrayList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j");
List<Double> doubleList = Lists.newArrayList(16.66, 16.66,16.66,16.66,16.66,16.7);
distributeDTO.setResourceList(strings); List<Long> longs = Lists.newArrayList(12360L, 12361L, 12362L, 12363L, 12364L, 12365L);
distributeDTO.setIsWeight(Boolean.TRUE); List<Double> doubleList = Lists.newArrayList(16.66, 16.66, 16.66, 16.66, 16.66, 16.7);
// distributeDTO.setResourceList(strings);
distributeDTO.setWeights(doubleList); distributeDTO.setWeights(doubleList);
distributeDTO.setDeptIds(longs); distributeDTO.setDeptIds(longs);
log.info(JSONUtil.toJsonStr(assignDataService.assignData(distributeDTO))); log.info(JSONUtil.toJsonStr(assignDataService.assignData(distributeDTO)));
@ -46,7 +46,6 @@ public class SettingTest {
} }
public static <T> List<List<T>> fixedGrouping2(List<T> source, int n) { public static <T> List<List<T>> fixedGrouping2(List<T> source, int n) {
if (null == source || source.size() == 0 || n <= 0) if (null == source || source.size() == 0 || n <= 0)
@ -173,9 +172,9 @@ public class SettingTest {
} }
@Test @Test
public void sortMap(){ public void sortMap() {
List<Double> doubles = Lists.newArrayList(123.00, 122.00); List<Double> doubles = Lists.newArrayList(123.00, 122.00);
List<Long> longs = Lists.newArrayList(12362L,12361L); List<Long> longs = Lists.newArrayList(12362L, 12361L);
Map<Long, Double> collect = longs.stream().collect(Collectors.toMap(key -> key, key -> doubles.get(longs.indexOf(key)))); Map<Long, Double> collect = longs.stream().collect(Collectors.toMap(key -> key, key -> doubles.get(longs.indexOf(key))));
TreeMap<Long, Double> sort = MapUtil.sort(collect); TreeMap<Long, Double> sort = MapUtil.sort(collect);

Loading…
Cancel
Save