代码优化
This commit is contained in:
+2
@@ -16,5 +16,7 @@ public class FangjiaConstant {
|
|||||||
public static final byte POTENTIAL_SEND_TYPE = 4;
|
public static final byte POTENTIAL_SEND_TYPE = 4;
|
||||||
|
|
||||||
public static final int SUCCESS_RESPONSE_CODE = 200;
|
public static final int SUCCESS_RESPONSE_CODE = 200;
|
||||||
|
|
||||||
|
public static final int SEND_THREAD_NUM = 100;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+32
-7
@@ -4,6 +4,8 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -50,6 +52,8 @@ public class CityRankingSendJob implements SimpleJob {
|
|||||||
|
|
||||||
private static final int NUM = 10;// 排名前10
|
private static final int NUM = 10;// 排名前10
|
||||||
|
|
||||||
|
private static final ExecutorService THREAD_POOL = Executors.newFixedThreadPool(FangjiaConstant.SEND_THREAD_NUM);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(ShardingContext context) {
|
public void execute(ShardingContext context) {
|
||||||
LOG.info("城市排名推送开始,context:" + context);
|
LOG.info("城市排名推送开始,context:" + context);
|
||||||
@@ -114,13 +118,7 @@ public class CityRankingSendJob implements SimpleJob {
|
|||||||
for (CityCollect city : cityList) {
|
for (CityCollect city : cityList) {
|
||||||
// cityList已排序好,如果cityName不变,则使用上个排名
|
// cityList已排序好,如果cityName不变,则使用上个排名
|
||||||
if (cityName.equals(city.getCity())) {
|
if (cityName.equals(city.getCity())) {
|
||||||
try {
|
THREAD_POOL.execute(new MyThread(city, topRankings, bottomRankings, potentialRankings));
|
||||||
baseSend(city, topRankings, FangjiaConstant.INCREASE_SEND_TYPE);
|
|
||||||
sendRankings(city, bottomRankings, FangjiaConstant.INCREASE_SEND_TYPE);
|
|
||||||
sendRankings(city, potentialRankings, FangjiaConstant.POTENTIAL_SEND_TYPE);
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOG.error("推送失败!", e);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// 如果cityName改变,则查询替换cityName和排名
|
// 如果cityName改变,则查询替换cityName和排名
|
||||||
cityName = city.getCity();
|
cityName = city.getCity();
|
||||||
@@ -204,4 +202,31 @@ public class CityRankingSendJob implements SimpleJob {
|
|||||||
citySendLogService.save(city.getCity(), city.getUid(), 0L, sendType);
|
citySendLogService.save(city.getCity(), city.getUid(), 0L, sendType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class MyThread extends Thread {
|
||||||
|
|
||||||
|
private Integer topRankings;
|
||||||
|
private Integer bottomRankings;
|
||||||
|
private Integer potentialRankings;
|
||||||
|
|
||||||
|
private CityCollect city;
|
||||||
|
|
||||||
|
public MyThread(CityCollect city, Integer topRankings, Integer bottomRankings, Integer potentialRankings) {
|
||||||
|
this.topRankings = topRankings;
|
||||||
|
this.bottomRankings = bottomRankings;
|
||||||
|
this.potentialRankings = potentialRankings;
|
||||||
|
this.city = city;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
baseSend(city, topRankings, FangjiaConstant.INCREASE_SEND_TYPE);
|
||||||
|
sendRankings(city, bottomRankings, FangjiaConstant.INCREASE_SEND_TYPE);
|
||||||
|
sendRankings(city, potentialRankings, FangjiaConstant.POTENTIAL_SEND_TYPE);
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOG.error("推送失败!", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-10
@@ -1,6 +1,8 @@
|
|||||||
package com.fangjia.job.youfang.job;
|
package com.fangjia.job.youfang.job;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -39,6 +41,8 @@ public class HousingEstateNewsSendJob implements SimpleJob {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private PushMessageService pushMessageService;
|
private PushMessageService pushMessageService;
|
||||||
|
|
||||||
|
private static final ExecutorService THREAD_POOL = Executors.newFixedThreadPool(FangjiaConstant.SEND_THREAD_NUM);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(ShardingContext context) {
|
public void execute(ShardingContext context) {
|
||||||
LOG.info("城市小区推送开始,context:" + context);
|
LOG.info("城市小区推送开始,context:" + context);
|
||||||
@@ -50,20 +54,12 @@ public class HousingEstateNewsSendJob implements SimpleJob {
|
|||||||
for (CityCollect city : cityList) {
|
for (CityCollect city : cityList) {
|
||||||
// cityList已排序好,如果cityName不变,则使用上个newsList
|
// cityList已排序好,如果cityName不变,则使用上个newsList
|
||||||
if (cityName.equals(city.getCity())) {
|
if (cityName.equals(city.getCity())) {
|
||||||
try {
|
THREAD_POOL.execute(new MyThread(city, newsList));
|
||||||
sendNews(city, newsList);
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOG.error("推送失败!", e);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// 如果cityName改变,则查询替换cityName和newsList
|
// 如果cityName改变,则查询替换cityName和newsList
|
||||||
cityName = city.getCity();
|
cityName = city.getCity();
|
||||||
newsList = newsService.findHousingEstate(cityName);
|
newsList = newsService.findHousingEstate(cityName);
|
||||||
try {
|
THREAD_POOL.execute(new MyThread(city, newsList));
|
||||||
sendNews(city, newsList);
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOG.error("推送失败!", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -94,7 +90,25 @@ public class HousingEstateNewsSendJob implements SimpleJob {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class MyThread extends Thread {
|
||||||
|
private List<News> news;
|
||||||
|
private CityCollect city;
|
||||||
|
|
||||||
|
public MyThread(CityCollect city, List<News> news) {
|
||||||
|
this.news = news;
|
||||||
|
this.city = city;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
sendNews(city, news);
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOG.error("推送失败!", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+31
-13
@@ -1,6 +1,9 @@
|
|||||||
package com.fangjia.job.youfang.job;
|
package com.fangjia.job.youfang.job;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@@ -31,13 +34,15 @@ public class TopNewsSendJob implements SimpleJob {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private NewsService newsService;
|
private NewsService newsService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private EnterpriseProductUserService enterpriseProductUserService;
|
private EnterpriseProductUserService enterpriseProductUserService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private PushMessageService pushMessageService;
|
private PushMessageService pushMessageService;
|
||||||
|
|
||||||
|
private static final ExecutorService THREAD_POOL = Executors.newFixedThreadPool(FangjiaConstant.SEND_THREAD_NUM);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(ShardingContext context) {
|
public void execute(ShardingContext context) {
|
||||||
LOG.info("城市置顶咨询推送开始,context:" + context);
|
LOG.info("城市置顶咨询推送开始,context:" + context);
|
||||||
@@ -49,20 +54,13 @@ public class TopNewsSendJob implements SimpleJob {
|
|||||||
for (CityCollect city : cityList) {
|
for (CityCollect city : cityList) {
|
||||||
// cityList已排序好,如果cityName不变,则使用上个newsList
|
// cityList已排序好,如果cityName不变,则使用上个newsList
|
||||||
if (cityName.equals(city.getCity())) {
|
if (cityName.equals(city.getCity())) {
|
||||||
try {
|
THREAD_POOL.execute(new MyThread(city, newsList));
|
||||||
sendNews(city, newsList);
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOG.error("推送失败!", e);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// 如果cityName改变,则查询替换cityName和newsList
|
// 如果cityName改变,则查询替换cityName和newsList
|
||||||
cityName = city.getCity();
|
cityName = city.getCity();
|
||||||
newsList = newsService.findTopNews(cityName);
|
newsList = newsService.findTopNews(cityName);
|
||||||
try {
|
THREAD_POOL.execute(new MyThread(city, newsList));
|
||||||
sendNews(city, newsList);
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOG.error("推送失败!", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -76,7 +74,8 @@ public class TopNewsSendJob implements SimpleJob {
|
|||||||
if (null == sendList || sendList.isEmpty()) {
|
if (null == sendList || sendList.isEmpty()) {
|
||||||
LOG.info("新闻推送开始。。。uid:" + city.getUid() + ",newsId:" + n.getId());
|
LOG.info("新闻推送开始。。。uid:" + city.getUid() + ",newsId:" + n.getId());
|
||||||
// 3.发送
|
// 3.发送
|
||||||
EnterpriseProductUser user = enterpriseProductUserService.getById(city.getUid(), new String[] { "push_client_id" });
|
EnterpriseProductUser user = enterpriseProductUserService.getById(city.getUid(),
|
||||||
|
new String[] { "push_client_id" });
|
||||||
String pushMsg = n.getContent();
|
String pushMsg = n.getContent();
|
||||||
LOG.info(pushMsg);
|
LOG.info(pushMsg);
|
||||||
PushMessageParam param = new PushMessageParam();
|
PushMessageParam param = new PushMessageParam();
|
||||||
@@ -94,4 +93,23 @@ public class TopNewsSendJob implements SimpleJob {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class MyThread extends Thread {
|
||||||
|
private List<News> news;
|
||||||
|
private CityCollect city;
|
||||||
|
|
||||||
|
public MyThread(CityCollect city, List<News> news) {
|
||||||
|
this.news = news;
|
||||||
|
this.city = city;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
sendNews(city, news);
|
||||||
|
} catch (Exception e) {
|
||||||
|
LOG.error("推送失败!", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user