package com.beagledata.mgc.lucas.jdcvmatch.openapi.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.beagledata.mgc.lucas.jdcvmatch.common.Result;
import com.beagledata.mgc.lucas.jdcvmatch.openapi.common.Constants;
import com.beagledata.mgc.lucas.jdcvmatch.openapi.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
/**
-
Created by Cyf on 2019/9/10
**/
@Service
public class SyncServiceImpl implements SyncService {
private static final Logger logger = LoggerFactory.getLogger(SyncServiceImpl.class);@Autowired
private JdService jdService;
@Autowired
private CvService cvService;
@Autowired
private RelationService relationService;@Override
public Result sync(String json) {
if (StringUtils.isEmpty(json)) {
logger.info(“数据同步,json为空”);
throw new IllegalArgumentException(“数据不能为空”);
}
JSONObject object = JSONObject.parseObject(json);
String category = object.getString(“category”);
if (StringUtils.isEmpty(category)) {
logger.info(“数据同步,category为空”);
throw new IllegalArgumentException(“同步类别不能为空”);
}
String status = object.getString(“status”);
if (StringUtils.isEmpty(status)) {
logger.info(“数据同步,status为空”);
throw new IllegalArgumentException(“同步状态类型不能为空”);
}
JSONObject data = object.getJSONObject(“data”);
if (data == null) {
logger.info(“数据同步,data为空”);
throw new IllegalArgumentException(“同步数据不能为空”);
}
try {
SyncExecutor executor;
if (Constants.CATEGORY_JD.equals(category)) {
executor = jdService;
} else if (Constants.CATEGORY_CV.equals(category)) {
executor = cvService;
} else if (Constants.CATEGORY_RELATION.equals(category)) {
executor = relationService;
} else {
logger.info(String.format(“数据同步,category的值异常: %s”, category));
return Result.newError().withMsg(“数据同步类别异常”);
}return excuteSync(executor, status, data.toJSONString()); } catch (Exception e) { logger.info(String.format("数据同步出错错,category: %s, status: %s, data:%s", category, status, data.toString())); logger.error(e.getLocalizedMessage(), e); throw new IllegalArgumentException("数据同步出错"); }
}
/**
- 执行数据同步
*/
private Result excuteSync(SyncExecutor executor, String status, String data) {
if (Constants.STATUS_CREATE.equals(status)) {
executor.add(data);
} else if (Constants.STATUS_DELETE.equals(status)) {
executor.delete(data);
} else if (Constants.STATUS_UPDATE.equals(status)) {
executor.update(data);
} else {
logger.info(String.format(“数据同步,status的值异常: %s”, status));
return Result.newError().withMsg(“同步数据状态出错”);
}
return Result.newSuccess();
}
- 执行数据同步
}
package com.beagledata.mgc.lucas.jdcvmatch.openapi.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.beagledata.mgc.lucas.jdcvmatch.common.Result;
import com.beagledata.mgc.lucas.jdcvmatch.common.domain.Relation;
import com.beagledata.mgc.lucas.jdcvmatch.openapi.mq.Producer;
import com.beagledata.mgc.lucas.jdcvmatch.common.QueueName;
import com.beagledata.mgc.lucas.jdcvmatch.openapi.service.RelationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Date;
import java.util.UUID;
/**
-
Created by Cyf on 2019/9/10
**/
@Service
public class RelationServiceImpl implements RelationService {
private static final Logger logger = LoggerFactory.getLogger(RelationServiceImpl.class);@Autowired
private Producer producer;
@Autowired
private MongoTemplate mongoTemplate;@Override
public Result add(String jdJson) {
producer.sendTextMsg(QueueName.RELATION_ADD, jdJson);
return Result.newSuccess();
}@Override
public Result delete(String deleteJson) {
return Result.newSuccess();
}@Override
public Result update(String updateJson) {
return Result.newSuccess();
}@Override
@JmsListener(destination = QueueName.RELATION_ADD)
public void consumerAdd(String text) {
logger.info(“consumerAdd 接收到的报文为:” + text);
try {
JSONObject json = JSONObject.parseObject(text);
if (json == null) {
logger.info(“relation数据同步 [新增], json为空”);
return;
}
String jdId = json.getString(“jdId”);
String cvId = json.getString(“cvId”);
if (StringUtils.isEmpty(jdId) || StringUtils.isEmpty(cvId)) {
logger.info(String.format(“relation数据同步 [新增], 参数为空, jdId = %s, cvId = %s”, jdId, cvId));
return;
}
Query query = new Query(Criteria.where(“jdId”).is(jdId).and(“cvId”).is(cvId));
Update update = new Update();
update.set(“jdId”, jdId);
update.set(“cvId”, cvId);
update.set(“createTime”, new Date());
mongoTemplate.upsert(query, update, Relation.class);
} catch (Exception e) {
logger.error(e.getLocalizedMessage(), e);
}
}@Override
// @JmsListener(destination = “”)
public void consumerDel(String text) {
logger.info(“consumerDel 接收到的报文为:” + text);
try {} catch (Exception e) { logger.error(e.getLocalizedMessage(), e); }
}
@Override
// @JmsListener(destination = “”)
public void consumerUpdate(String text) {
logger.info(“consumerUpdate 接收到的报文为:” + text);
try {} catch (Exception e) { logger.error(e.getLocalizedMessage(), e); }
}
}
另外两个就是activeMQ增删改查的例子