From e80b37a25ce85762a32b3f7d901d415a8ad47d44 Mon Sep 17 00:00:00 2001 From: e Date: Tue, 21 May 2024 19:23:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=95=E7=BA=BF=E7=A8=8B=E6=8E=A8=E9=80=81?= =?UTF-8?q?=EF=BC=8C=E6=95=B0=E6=8D=AE=E4=BD=93=E9=87=8F1W4=E4=B8=8D?= =?UTF-8?q?=E5=88=B0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 21 +++++++++ pom.xml | 40 +++++++++++++++++ .../com/mashibing/FutureTaskStarterApp.java | 20 +++++++++ src/main/java/com/mashibing/entity/Air.java | 19 ++++++++ .../java/com/mashibing/mapper/AirMapper.java | 19 ++++++++ .../com/mashibing/service/PushService.java | 11 +++++ .../service/impl/PushServiceImpl.java | 45 +++++++++++++++++++ .../java/com/mashibing/util/PushUtil.java | 23 ++++++++++ src/main/resources/application.yml | 8 ++++ .../mashibing/service/PushServiceTest.java | 22 +++++++++ 10 files changed, 228 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/com/mashibing/FutureTaskStarterApp.java create mode 100644 src/main/java/com/mashibing/entity/Air.java create mode 100644 src/main/java/com/mashibing/mapper/AirMapper.java create mode 100644 src/main/java/com/mashibing/service/PushService.java create mode 100644 src/main/java/com/mashibing/service/impl/PushServiceImpl.java create mode 100644 src/main/java/com/mashibing/util/PushUtil.java create mode 100644 src/main/resources/application.yml create mode 100644 src/test/java/com/mashibing/service/PushServiceTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8450319 --- /dev/null +++ b/.gitignore @@ -0,0 +1,21 @@ +# Created by .ignore support plugin (hsz.mobi) +### Example user template template +### Example user template + +# IntelliJ project files +.idea +*.iml +out +gen +### Maven template +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a336839 --- /dev/null +++ b/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.3.12.RELEASE + + + com.mashibing + futuretask + 1.0-SNAPSHOT + + + + org.springframework.boot + spring-boot-starter-web + + + mysql + mysql-connector-java + 5.1.47 + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 2.2.2 + + + org.projectlombok + lombok + + + org.springframework.boot + spring-boot-starter-test + + + \ No newline at end of file diff --git a/src/main/java/com/mashibing/FutureTaskStarterApp.java b/src/main/java/com/mashibing/FutureTaskStarterApp.java new file mode 100644 index 0000000..83043e6 --- /dev/null +++ b/src/main/java/com/mashibing/FutureTaskStarterApp.java @@ -0,0 +1,20 @@ +package com.mashibing; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import java.util.concurrent.FutureTask; + +/** + * @author zjw + * @description + */ +@SpringBootApplication +@MapperScan(basePackages = "com.mashibing.mapper") +public class FutureTaskStarterApp { + + public static void main(String[] args) { + SpringApplication.run(FutureTaskStarterApp.class,args); + } +} diff --git a/src/main/java/com/mashibing/entity/Air.java b/src/main/java/com/mashibing/entity/Air.java new file mode 100644 index 0000000..e84a066 --- /dev/null +++ b/src/main/java/com/mashibing/entity/Air.java @@ -0,0 +1,19 @@ +package com.mashibing.entity; + +import lombok.Data; + +import java.io.Serializable; + +/** + * @author zjw + * @description + */ +@Data +public class Air implements Serializable { + + private Long id; + + private int pm25; + + private String monitoringStation; +} diff --git a/src/main/java/com/mashibing/mapper/AirMapper.java b/src/main/java/com/mashibing/mapper/AirMapper.java new file mode 100644 index 0000000..c0059ff --- /dev/null +++ b/src/main/java/com/mashibing/mapper/AirMapper.java @@ -0,0 +1,19 @@ +package com.mashibing.mapper; + +import com.mashibing.entity.Air; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * @author zjw + * @description + */ +public interface AirMapper { + + @Select("select * from air") + List findAll(); + + +} diff --git a/src/main/java/com/mashibing/service/PushService.java b/src/main/java/com/mashibing/service/PushService.java new file mode 100644 index 0000000..79afc88 --- /dev/null +++ b/src/main/java/com/mashibing/service/PushService.java @@ -0,0 +1,11 @@ +package com.mashibing.service; + +/** + * @author zjw + * @description + */ +public interface PushService { + + void pushData(); + +} diff --git a/src/main/java/com/mashibing/service/impl/PushServiceImpl.java b/src/main/java/com/mashibing/service/impl/PushServiceImpl.java new file mode 100644 index 0000000..8da8859 --- /dev/null +++ b/src/main/java/com/mashibing/service/impl/PushServiceImpl.java @@ -0,0 +1,45 @@ +package com.mashibing.service.impl; + +import com.mashibing.entity.Air; +import com.mashibing.mapper.AirMapper; +import com.mashibing.service.PushService; +import com.mashibing.util.PushUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.List; + +/** + * @author zjw + * @description + */ +@Service +@Slf4j +public class PushServiceImpl implements PushService { + + @Autowired + private PushUtil pushUtil; + + @Resource + private AirMapper airMapper; + + @Override + public void pushData() { + long start = System.currentTimeMillis(); + List errorData = new ArrayList<>(); + List airList = airMapper.findAll(); + log.info("【推送数据】 推送air数据条数 = {}",airList.size()); + for (Air air : airList) { + boolean success = pushUtil.push(air); + if(!success){ + errorData.add(air); + // 插入推送失败日志。 + } + } + long end = System.currentTimeMillis(); + log.info("【推送数据】 推送air数据条数 = {},花费时间 = {}",airList.size() - errorData.size(),(end - start) / 1000); + } +} diff --git a/src/main/java/com/mashibing/util/PushUtil.java b/src/main/java/com/mashibing/util/PushUtil.java new file mode 100644 index 0000000..dc80a52 --- /dev/null +++ b/src/main/java/com/mashibing/util/PushUtil.java @@ -0,0 +1,23 @@ +package com.mashibing.util; + +import com.mashibing.entity.Air; +import org.springframework.stereotype.Component; + +/** + * @author zjw + * @description + */ +@Component +public class PushUtil { + + + public boolean push(Air air){ + try { + Thread.sleep(1); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return true; + } + +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..2f3ea4b --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,8 @@ +spring: + datasource: + driver-class-name: com.mysql.jdbc.Driver + url: jdbc:mysql://localhost:3306/air + +mybatis: + configuration: + map-underscore-to-camel-case: true diff --git a/src/test/java/com/mashibing/service/PushServiceTest.java b/src/test/java/com/mashibing/service/PushServiceTest.java new file mode 100644 index 0000000..c94a3d0 --- /dev/null +++ b/src/test/java/com/mashibing/service/PushServiceTest.java @@ -0,0 +1,22 @@ +package com.mashibing.service; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.junit.Assert.*; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class PushServiceTest { + + @Autowired + private PushService pushService; + + @Test + public void pushData() { + pushService.pushData(); + } +} \ No newline at end of file