Skip to content

Commit

Permalink
feat remote remote interface (#255)
Browse files Browse the repository at this point in the history
Co-authored-by: Terrence<[email protected]>
  • Loading branch information
TerrenceGee authored Apr 15, 2024
1 parent ff88353 commit 00e3201
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class RemoteDataResourceEvent {
private Long resourceId;
private Integer resourceState;
}
//@Data
//@AllArgsConstructor
//@NoArgsConstructor
//public class RemoteDataResourceEvent {
// private Long resourceId;
// private Integer resourceState;
//}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.primihub.biz.entity.data.po.*;
import com.primihub.biz.entity.data.req.*;
import com.primihub.biz.entity.data.vo.*;
import com.primihub.biz.entity.event.RemoteDataResourceEvent;
import com.primihub.biz.entity.sys.po.SysFile;
import com.primihub.biz.entity.sys.po.SysLocalOrganInfo;
import com.primihub.biz.entity.sys.po.SysUser;
Expand Down Expand Up @@ -188,10 +187,6 @@ public BaseResultEntity saveDataResource(DataResourceReq req,Long userId){
map.put("resourceName",dataResource.getResourceName());
map.put("resourceDesc",dataResource.getResourceDesc());

// 传送任务
if (dataResource.getResourceAuthType().equals(DataResourceAuthType.PUBLIC.getAuthType())) {
applicationContext.publishEvent(new RemoteDataResourceEvent(dataResource.getResourceId(), null));
}
}catch (Exception e){
log.info("save DataResource Exception:{}",e.getMessage());
e.printStackTrace();
Expand Down Expand Up @@ -242,17 +237,6 @@ public BaseResultEntity editDataResource(DataResourceReq req, Long userId) {
map.put("resourceName",dataResource.getResourceName());
map.put("resourceDesc",dataResource.getResourceDesc());

if (Objects.equals(dataResource.getResourceAuthType(), DataResourceAuthType.PUBLIC.getAuthType()) || Objects.equals(req.getResourceAuthType(), DataResourceAuthType.PRIVATE.getAuthType())) {
if (Objects.equals(req.getResourceAuthType(), DataResourceAuthType.PUBLIC.getAuthType())) {
RemoteDataResourceEvent remoteDataResourceEvent = new RemoteDataResourceEvent(dataResource.getResourceId(), ResourceStateEnum.AVAILABLE.getStateType());
applicationContext.publishEvent(remoteDataResourceEvent);
log.info("spring event publish : {}", JSONObject.toJSONString(remoteDataResourceEvent));
} else {
RemoteDataResourceEvent remoteDataResourceEvent = new RemoteDataResourceEvent(dataResource.getResourceId(), ResourceStateEnum.NOT_AVAILABLE.getStateType());
applicationContext.publishEvent(new RemoteDataResourceEvent(dataResource.getResourceId(), ResourceStateEnum.NOT_AVAILABLE.getStateType()));
log.info("spring event publish : {}", JSONObject.toJSONString(remoteDataResourceEvent));
}
}
return BaseResultEntity.success(map);
}

Expand Down Expand Up @@ -730,7 +714,6 @@ public BaseResultEntity resourceStatusChange(Long resourceId, Integer resourceSt
fusionResourceService.saveResource(organConfiguration.getSysLocalOrganId(),findCopyResourceList(dataResource.getResourceId(), dataResource.getResourceId()));
singleTaskChannel.input().send(MessageBuilder.withPayload(JSON.toJSONString(new BaseFunctionHandleEntity(BaseFunctionHandleEnum.SINGLE_DATA_FUSION_RESOURCE_TASK.getHandleType(),dataResource))).build());

applicationContext.publishEvent(new RemoteDataResourceEvent(resourceId, null));
}
return BaseResultEntity.success();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,81 +1,81 @@
package com.primihub.biz.service.share;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.primihub.biz.entity.event.RemoteDataResourceEvent;
import com.primihub.biz.service.data.DataResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.*;


@Service
@Slf4j
public class RemoteShareService {
private static final String remoteAddress2 = "http://192.168.99.12:31210";
private static final String remoteAddress1 = "http://27.185.28.52:32210";
private static final String remoteUrl = "/dataShare/share";
@Resource
private Environment environment;
@Resource(name="soaRestTemplate")
private RestTemplate restTemplate;
@Autowired
DataResourceService resourceService;
/**
* 什么时候调用:
* 1.创建资源+公开
* 2.资源进行上下线+公开
* 3.修改资源
* 1).公开->私有或者授权
* 2).私有或者授权 -> 公开
*/
@org.springframework.context.event.EventListener(RemoteDataResourceEvent.class)
public void transDataResource(RemoteDataResourceEvent event) {
log.info("spring event 接受的数据: {}", JSON.toJSONString(event));
Long resourceId = event.getResourceId();
if (resourceId == null) {
log.error("本次数据资源传送 没有数据id 请修改代码逻辑");
}
Map transMap = resourceService.getDataResourceToTransfer(event.getResourceId(), event.getResourceState());
if (transMap==null) {
log.info("未找到id: {} 的数据资源,传送失败", event.getResourceId());
}
transDataResource(resourceId, transMap, 1);
}

public void transDataResource(Long resourceId, Map transMap, int count) {
String remoteAddress;
String profile = environment.getProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME);
log.info("当前的profile: {}", profile);
if (profile == null) {
profile = "";
}
if (profile.trim().startsWith("test") || profile.trim().startsWith("dev")) {
remoteAddress = remoteAddress2;
} else {
remoteAddress = remoteAddress1;
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Map<String, Object>> request = new HttpEntity(transMap, headers);
log.info("传送地址:{}, 传送的资源Id: {}", remoteAddress+remoteUrl, resourceId);
Map map = restTemplate.postForObject(remoteAddress + remoteUrl, request, Map.class);
log.info("传送地址:{}, 传送的资源Id: {}, 返回结果:{}", remoteAddress+remoteUrl, resourceId, JSONObject.toJSONString(map));
if (map.get("code") == null || !map.get("code").toString().equals("0")) {
if (count<=3) {
transDataResource(resourceId, transMap, count+1);
}
log.info("传送地址:{}, 传送的资源Id: {},重试次数用完,未传送成功", remoteAddress+remoteUrl, resourceId);
}
}

}
//package com.primihub.biz.service.share;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import com.primihub.biz.entity.event.RemoteDataResourceEvent;
//import com.primihub.biz.service.data.DataResourceService;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.core.env.AbstractEnvironment;
//import org.springframework.core.env.Environment;
//import org.springframework.http.HttpEntity;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.MediaType;
//import org.springframework.stereotype.Service;
//import org.springframework.web.client.RestTemplate;
//
//import javax.annotation.Resource;
//import java.util.*;
//
//
//@Service
//@Slf4j
//public class RemoteShareService {
// private static final String remoteAddress2 = "http://192.168.99.12:31210";
// private static final String remoteAddress1 = "http://27.185.28.52:32210";
// private static final String remoteUrl = "/dataShare/share";
// @Resource
// private Environment environment;
// @Resource(name="soaRestTemplate")
// private RestTemplate restTemplate;
// @Autowired
// DataResourceService resourceService;
// /**
// * 什么时候调用:
// * 1.创建资源+公开
// * 2.资源进行上下线+公开
// * 3.修改资源
// * 1).公开->私有或者授权
// * 2).私有或者授权 -> 公开
// */
// @org.springframework.context.event.EventListener(RemoteDataResourceEvent.class)
// public void transDataResource(RemoteDataResourceEvent event) {
// log.info("spring event 接受的数据: {}", JSON.toJSONString(event));
// Long resourceId = event.getResourceId();
// if (resourceId == null) {
// log.error("本次数据资源传送 没有数据id 请修改代码逻辑");
// }
// Map transMap = resourceService.getDataResourceToTransfer(event.getResourceId(), event.getResourceState());
// if (transMap==null) {
// log.info("未找到id: {} 的数据资源,传送失败", event.getResourceId());
// }
// transDataResource(resourceId, transMap, 1);
// }
//
// public void transDataResource(Long resourceId, Map transMap, int count) {
// String remoteAddress;
// String profile = environment.getProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME);
// log.info("当前的profile: {}", profile);
// if (profile == null) {
// profile = "";
// }
// if (profile.trim().startsWith("test") || profile.trim().startsWith("dev")) {
// remoteAddress = remoteAddress2;
// } else {
// remoteAddress = remoteAddress1;
// }
// HttpHeaders headers = new HttpHeaders();
// headers.setContentType(MediaType.APPLICATION_JSON);
// HttpEntity<Map<String, Object>> request = new HttpEntity(transMap, headers);
// log.info("传送地址:{}, 传送的资源Id: {}", remoteAddress+remoteUrl, resourceId);
// Map map = restTemplate.postForObject(remoteAddress + remoteUrl, request, Map.class);
// log.info("传送地址:{}, 传送的资源Id: {}, 返回结果:{}", remoteAddress+remoteUrl, resourceId, JSONObject.toJSONString(map));
// if (map.get("code") == null || !map.get("code").toString().equals("0")) {
// if (count<=3) {
// transDataResource(resourceId, transMap, count+1);
// }
// log.info("传送地址:{}, 传送的资源Id: {},重试次数用完,未传送成功", remoteAddress+remoteUrl, resourceId);
// }
// }
//
//}

0 comments on commit 00e3201

Please sign in to comment.