作者 王勇

在controller层对异常进行捕获

... ... @@ -59,11 +59,15 @@ public class BusExchangeController {
* @return {@link ResultJson}
*/
@DeleteMapping("/delete")
public ResultJson deleteBusExchange(@RequestBody BusExchange busExchange) throws IOException, TimeoutException
public ResultJson deleteBusExchange(@RequestBody BusExchange busExchange)
{
return busExchangeService.deleteByPrimaryKey(busExchange.getId()) > 0
? new ResultJson<>("200", "删除MQ交换机,成功")
: new ResultJson<>("500", "删除MQ交换机,失败");
try {
return busExchangeService.deleteByPrimaryKey(busExchange.getId()) > 0
? new ResultJson<>("200", "删除MQ交换机,成功")
: new ResultJson<>("500", "删除MQ交换机,失败");
} catch (IOException | TimeoutException e) {
return new ResultJson<>("500", "服务器异常,删除MQ交换机,失败");
}
}
/**
... ... @@ -73,11 +77,15 @@ public class BusExchangeController {
* @return {@link ResultJson}
*/
@GetMapping("/batchRemove")
public ResultJson batchRemoveBusExchange(String ids) throws IOException, TimeoutException
public ResultJson batchRemoveBusExchange(String ids)
{
return busExchangeService.deleteByPrimaryKey(ids) > 0
? new ResultJson<>("200", "批量删除MQ交换机,成功")
: new ResultJson<>("500", "批量删除MQ交换机,失败");
try {
return busExchangeService.deleteByPrimaryKey(ids) > 0
? new ResultJson<>("200", "批量删除MQ交换机,成功")
: new ResultJson<>("500", "批量删除MQ交换机,失败");
} catch (IOException | TimeoutException e) {
return new ResultJson<>("500", "服务器异常,批量删除MQ交换机,失败");
}
}
/**
... ... @@ -106,17 +114,21 @@ public class BusExchangeController {
* @return {@link ResultJson}
*/
@PostMapping("/insert")
public ResultJson insertBusExchange(@RequestBody BusExchange busExchange) throws IOException, TimeoutException
public ResultJson insertBusExchange(@RequestBody BusExchange busExchange)
{
//先验证,增加的虚拟主机的核心信息(交换机名称)是否已存在
String message = validateBusExchange(busExchange);
// 设置id
busExchange.setId(IdUtils.generateId());
return message == null
? busExchangeService.insertSelective(busExchange) > 0
? new ResultJson<>("200", "新增MQ交换机信息,成功")
: new ResultJson<>("500", "新增MQ交换机信息,失败")
: new ResultJson<>("400", message);
try {
//先验证,增加的虚拟主机的核心信息(交换机名称)是否已存在
String message = validateBusExchange(busExchange);
// 设置id
busExchange.setId(IdUtils.generateId());
return message == null
? busExchangeService.insertSelective(busExchange) > 0
? new ResultJson<>("200", "新增MQ交换机信息,成功")
: new ResultJson<>("500", "新增MQ交换机信息,失败")
: new ResultJson<>("400", message);
} catch (IOException | TimeoutException e) {
return new ResultJson<>("500", "新增MQ交换机信息,失败");
}
}
... ...
... ... @@ -67,11 +67,15 @@ public class BusQueueController {
* @return {@link ResultJson}
*/
@DeleteMapping("/delete")
public ResultJson deleteBusQueue(@RequestBody BusQueue busQueue) throws IOException, TimeoutException
public ResultJson deleteBusQueue(@RequestBody BusQueue busQueue)
{
return busQueueService.deleteByPrimaryKey(busQueue.getId()) > 0
? new ResultJson<>("200", "删除消息队列,成功")
: new ResultJson<>("500", "删除消息队列,失败");
try {
return busQueueService.deleteByPrimaryKey(busQueue.getId()) > 0
? new ResultJson<>("200", "删除消息队列,成功")
: new ResultJson<>("500", "删除消息队列,失败");
} catch (IOException | TimeoutException e) {
return new ResultJson<>("500", "服务器异常,删除消息队列,失败");
}
}
/**
... ... @@ -81,11 +85,15 @@ public class BusQueueController {
* @return {@link ResultJson}
*/
@GetMapping("/batchRemove")
public ResultJson batchRemoveBusQueue(String ids) throws IOException, TimeoutException
public ResultJson batchRemoveBusQueue(String ids)
{
return busQueueService.deleteByPrimaryKey(ids) > 0
? new ResultJson<>("200", "批量删除消息队列,成功")
: new ResultJson<>("500", "批量删除消息队列,失败");
try {
return busQueueService.deleteByPrimaryKey(ids) > 0
? new ResultJson<>("200", "批量删除MQ消息队列,成功")
: new ResultJson<>("500", "批量删除MQ消息队列,失败");
} catch (IOException | TimeoutException e) {
return new ResultJson<>("500", "服务器异常,批量删除MQ消息队列,失败");
}
}
/**
... ... @@ -101,8 +109,8 @@ public class BusQueueController {
String message = validateBusQueue(busQueue);
return message == null
? busQueueService.updateByPrimaryKeySelective(busQueue) > 0
? new ResultJson<>("200", "编辑消息队列信息,成功")
: new ResultJson<>("500", "编辑消息队列信息,失败")
? new ResultJson<>("200", "编辑MQ消息队列信息,成功")
: new ResultJson<>("500", "编辑MQ消息队列信息,失败")
: new ResultJson<>("400", message);
}
... ... @@ -113,13 +121,17 @@ public class BusQueueController {
* @return {@link ResultJson}
*/
@PostMapping("/insert")
public ResultJson insertBusQueue(@RequestBody BusQueue busQueue) throws IOException, TimeoutException
public ResultJson insertBusQueue(@RequestBody BusQueue busQueue)
{
String message = validateBusQueue(busQueue);
//验证通过
return message == null
? busQueueService.insertSelective(busQueue)
: new ResultJson<>("400", message);
try {
String message = validateBusQueue(busQueue);
//验证通过
return message == null
? busQueueService.insertSelective(busQueue)
: new ResultJson<>("400", message);
} catch (IOException | TimeoutException e) {
return new ResultJson<>("500", "服务器异常,新增MQ消息队列信息,失败");
}
}
/**
... ...
... ... @@ -58,11 +58,15 @@ public class BusServerController {
* @return {@link ResultJson}
*/
@DeleteMapping("/delete")
public ResultJson deleteBusServer(@RequestBody BusServer busServer) throws IOException, TimeoutException
public ResultJson deleteBusServer(@RequestBody BusServer busServer)
{
return busServerService.deleteByPrimaryKey(busServer.getId()) > 0
? new ResultJson<>("200", "删除MQ服务器,成功")
: new ResultJson<>("500", "删除MQ服务器,失败");
try {
return busServerService.deleteByPrimaryKey(busServer.getId()) > 0
? new ResultJson<>("200", "删除MQ服务器,成功")
: new ResultJson<>("500", "删除MQ服务器,失败");
} catch (IOException | TimeoutException e) {
return new ResultJson<>("500", "服务器异常,删除MQ服务器失败");
}
}
/**
... ... @@ -72,11 +76,15 @@ public class BusServerController {
* @return {@link ResultJson}
*/
@GetMapping("/batchRemove")
public ResultJson batchRemoveBusServer(String ids) throws IOException, TimeoutException
public ResultJson batchRemoveBusServer(String ids)
{
return busServerService.deleteByPrimaryKey(ids) > 0
? new ResultJson<>("200", "批量删除服务器,成功")
: new ResultJson<>("500", "批量删除服务器,失败");
try {
return busServerService.deleteByPrimaryKey(ids) > 0
? new ResultJson<>("200", "批量删除服务器,成功")
: new ResultJson<>("500", "批量删除服务器,失败");
} catch (IOException | TimeoutException e) {
return new ResultJson<>("500", "服务器异常,批量删除服务器,失败");
}
}
/**
... ... @@ -162,7 +170,7 @@ public class BusServerController {
* 则校验 该 ServerIp以及ServerPort(同时) 是否已存在
*/
if (!oldBusServer.getServerPort().equals(busServer.getServerPort()) ||
!oldBusServer.getServerIp().equals(busServer.getServerIp())) {
!oldBusServer.getServerIp().equals(busServer.getServerIp())) {
// 根据 ServerIp以及ServerPort(同时),进行查询校验
List<BusServer> info = busServerService.validateBusServer(busServer);
... ...
... ... @@ -12,6 +12,7 @@ import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeoutException;
/**
* @author 子诚
... ... @@ -151,11 +152,15 @@ public class MessageNoteController {
* @return {@link ResultJson}
*/
@PostMapping("/insert")
public ResultJson insertMessageNote(@RequestBody @NotNull MessageNote messageNote) throws Exception
public ResultJson insertMessageNote(@RequestBody @NotNull MessageNote messageNote)
{
return messageNoteService.insertSelective(messageNote) > 0
? new ResultJson<>("200", "编辑-消息收发记录,成功")
: new ResultJson<>("500", "编辑-消息收发记录,失败");
try {
return messageNoteService.insertSelective(messageNote) > 0
? new ResultJson<>("200", "新增-消息收发记录,成功")
: new ResultJson<>("500", "新增-消息收发记录,失败");
} catch (IOException | TimeoutException e) {
return new ResultJson<>("500", "服务器异常,编辑-消息收发记录,失败");
}
}
/**
... ...
... ... @@ -84,37 +84,41 @@ public class RabbitController {
@RequestParam(value = "SEQN", required = false) String SEQN,
@RequestParam(value = "VSHT") String VSHT,
@RequestParam(value = "SERV") String SERV,
@RequestParam(value = "content") String content) throws Exception
@RequestParam(value = "content") String content)
{
// 1、获取数据
XmlData xmlData = XmlData.builder()
.sender(SNDR)
.queueName(RCVR)
.sendDateTime(DDTM)
.exchangeName(TYPE)
.routingKeyName(STYP)
.token(TOKN)
.sequence(SEQN)
.virtualHostName(VSHT)
.serverName(SERV)
.sendContent(content)
.build();
// 2、校验格式、数据
ResultJson resultJson = xmlUtils.checkFormatAndData(xmlData);
if (!"200".equals(resultJson.getCode())) {
return resultJson;
}
XmlData sentData = (XmlData) resultJson.getData();
// 3、通过格式校验之后,进行配置校验
Boolean binding = userMessageBindingService.validateXmlBinding(sentData);
if (!binding) {
return ResultJson.error(CustomExceptionType.BINDING_ERROR);
try {
// 1、获取数据
XmlData xmlData = XmlData.builder()
.sender(SNDR)
.queueName(RCVR)
.sendDateTime(DDTM)
.exchangeName(TYPE)
.routingKeyName(STYP)
.token(TOKN)
.sequence(SEQN)
.virtualHostName(VSHT)
.serverName(SERV)
.sendContent(content)
.build();
// 2、校验格式、数据
ResultJson resultJson = xmlUtils.checkFormatAndData(xmlData);
if (!"200".equals(resultJson.getCode())) {
return resultJson;
}
XmlData sentData = (XmlData) resultJson.getData();
// 3、通过格式校验之后,进行配置校验
Boolean binding = userMessageBindingService.validateXmlBinding(sentData);
if (!binding) {
return ResultJson.error(CustomExceptionType.BINDING_ERROR);
}
// 4、mq发送消息,数据库中保存消息并保存至ES
return sendAndSave(sentData);
} catch (IOException | TimeoutException e) {
return ResultJson.error(CustomExceptionType.SERVER_EXCEPTION);
}
// 4、mq发送消息,数据库中保存消息并保存至ES
return sendAndSave(sentData);
}
public ResultJson sendAndSave(XmlData sentData) throws Exception
public ResultJson sendAndSave(XmlData sentData) throws IOException, TimeoutException
{
// 4、mq发送消息,数据库中保存消息
ResultJson result = directUtils.sendMessage(sentData);
... ...
... ... @@ -59,11 +59,15 @@ public class RoutingKeyController {
* @return {@link ResultJson}
*/
@DeleteMapping("/delete")
public ResultJson deleteRoutingKey(@RequestBody RoutingKey routingKey) throws IOException, TimeoutException
public ResultJson deleteRoutingKey(@RequestBody RoutingKey routingKey)
{
return routingKeyService.deleteByPrimaryKey(routingKey.getId()) > 0
? new ResultJson<>("200", "删除路由键,成功")
: new ResultJson<>("500", "删除路由键,失败");
try {
return routingKeyService.deleteByPrimaryKey(routingKey.getId()) > 0
? new ResultJson<>("200", "删除路由键,成功")
: new ResultJson<>("500", "删除路由键,失败");
} catch (IOException | TimeoutException e) {
return new ResultJson<>("500", "服务器异常,删除路由键,失败");
}
}
/**
... ... @@ -73,11 +77,15 @@ public class RoutingKeyController {
* @return {@link ResultJson}
*/
@GetMapping("/batchRemove")
public ResultJson batchRemoveRoutingKey(String ids) throws IOException, TimeoutException
public ResultJson batchRemoveRoutingKey(String ids)
{
return routingKeyService.deleteByPrimaryKey(ids) > 0
? new ResultJson<>("200", "删除路由键,成功")
: new ResultJson<>("500", "删除路由键,失败");
try {
return routingKeyService.deleteByPrimaryKey(ids) > 0
? new ResultJson<>("200", "删除路由键,成功")
: new ResultJson<>("500", "删除路由键,失败");
} catch (IOException | TimeoutException e) {
return new ResultJson<>("500", "服务器异常,删除路由键,失败");
}
}
/**
... ... @@ -108,7 +116,6 @@ public class RoutingKeyController {
@PostMapping("/insert")
public ResultJson insertRoutingKey(@RequestBody RoutingKey routingKey)
{
//先验证,增加的服务器的核心信息(ip和port,同时存在)是否已存在
String message = validateRoutingKey(routingKey);
// 设置id
... ... @@ -134,7 +141,6 @@ public class RoutingKeyController {
}
// 如果id不为空,则是 update 操作
if (routingKey.getId() != null && routingKey.getId().length() != 0) {
// 根据 id 查询出原来的 路由键 信息
RoutingKey oldRoutingKey = routingKeyService.selectByPrimaryKey(routingKey.getId());
... ...
... ... @@ -86,9 +86,13 @@ public class UserInfoController {
* @return
*/
@PutMapping("/updatePassword")
public ResultJson updatePassword(@RequestBody UserInfo userInfo) throws IOException, URISyntaxException
public ResultJson updatePassword(@RequestBody UserInfo userInfo)
{
return userInfoService.updatePassword(userInfo);
try {
return userInfoService.updatePassword(userInfo);
} catch (IOException | URISyntaxException e) {
return new ResultJson<>("500", "服务器异常,修改密码失败");
}
}
/**
... ...
... ... @@ -75,11 +75,14 @@ public class UserMessageBindingController {
*/
@DeleteMapping("/delete")
public ResultJson deleteUserMessageBinding(@RequestBody UserMessageBinding userMessageBinding)
throws IOException, TimeoutException
{
return userMessageBindingService.deleteByPrimaryKey(userMessageBinding.getId()) > 0
? new ResultJson<>("200", "删除-账户消息配置信息,成功")
: new ResultJson<>("500", "删除-账户消息配置信息,失败");
try {
return userMessageBindingService.deleteByPrimaryKey(userMessageBinding.getId()) > 0
? new ResultJson<>("200", "删除-用户户消息配置信息,成功")
: new ResultJson<>("500", "删除-用户户消息配置信息,失败");
} catch (IOException | TimeoutException e) {
return new ResultJson<>("500", "服务器异常,删除-用户户消息配置信息,失败");
}
}
/**
... ... @@ -90,11 +93,14 @@ public class UserMessageBindingController {
*/
@GetMapping("/batchRemove")
public ResultJson batchRemoveUserMessageBinding(String ids)
throws IOException, TimeoutException
{
return userMessageBindingService.deleteByPrimaryKey(ids) > 0
? new ResultJson<>("200", "批量删除-账户消息配置信息,成功")
: new ResultJson<>("500", "批量删除-账户消息配置信息,失败");
try {
return userMessageBindingService.deleteByPrimaryKey(ids) > 0
? new ResultJson<>("200", "批量删除-账户消息配置信息,成功")
: new ResultJson<>("500", "批量删除-账户消息配置信息,失败");
} catch (IOException | TimeoutException e) {
return new ResultJson<>("500", "服务器异常,批量删除-用户户消息配置信息,失败");
}
}
/**
... ... @@ -119,10 +125,13 @@ public class UserMessageBindingController {
*/
@PostMapping("/insert")
public ResultJson insertUserMessageBinding(@RequestBody UserMessageBinding userMessageBinding)
throws IOException, TimeoutException
{
return userMessageBindingService.insertSelective(userMessageBinding) > 0
? new ResultJson<>("200", "添加-账户消息配置-信息,成功")
: new ResultJson<>("500", "添加-账户消息配置-信息,失败");
try {
return userMessageBindingService.insertSelective(userMessageBinding) > 0
? new ResultJson<>("200", "新增-账户消息配置-信息,成功")
: new ResultJson<>("500", "新增-账户消息配置-信息,失败");
} catch (IOException | TimeoutException e) {
return new ResultJson<>("500", "服务器异常,新增-用户户消息配置信息,失败");
}
}
}
... ...
... ... @@ -27,6 +27,8 @@ public enum CustomExceptionType {
EXCHANGE_NO_EXIST("20405", "报文数据错误,交换机不存在!"),
ROUTING_KEY_NO_EXIST("20406", "报文数据错误,路由键不存在!"),
SERVER_EXCEPTION("50500", "服务器异常,发送消息失败!"),
CLIENT_ERROR("400", "客户端异常"),
SYSTEM_ERROR("500", "系统服务异常"),
OTHER_ERROR("999", "其他未知异常");
... ...
... ... @@ -94,7 +94,7 @@ public class DirectUtils {
* @return 返回链接
* @throws Exception
*/
public static Connection getConnection(String hostIp, int hostPort, String vHostName, String userName, String password) throws Exception
public static Connection getConnection(String hostIp, int hostPort, String vHostName, String userName, String password) throws IOException, TimeoutException
{
String base = EncryptionUtils.decryptBase64(password);
String[] split = base.split("\\.");
... ... @@ -243,7 +243,7 @@ public class DirectUtils {
}
public ResultJson sendMessage(XmlData xmlData) throws Exception
public ResultJson sendMessage(XmlData xmlData) throws IOException, TimeoutException
{
/**
* 可以在这里根据类型的不同,进行不同的消息发送
... ... @@ -253,15 +253,16 @@ public class DirectUtils {
/**
* 发送消息,使用中
*
* @param xmlData {@link XmlData}
* @return
* @throws Exception
*/
public ResultJson directProducer(XmlData xmlData) throws Exception
public ResultJson directProducer(XmlData xmlData) throws IOException, TimeoutException
{
// 1、创建Connection
Connection connection = getConnection(xmlData.getServerIp(), xmlData.getServerPort(),
xmlData.getVirtualHostName(),xmlData.getSuperUsername(), xmlData.getSuperPassword());
xmlData.getVirtualHostName(), xmlData.getSuperUsername(), xmlData.getSuperPassword());
// 2、 通过Connection创建一个新的Channel
Channel channel = connection.createChannel();
// 3、开启消息的确认机制(confirm:保证消息能够发送到 exchange)
... ...
... ... @@ -4,6 +4,9 @@ import com.github.pagehelper.PageInfo;
import com.sunyo.wlpt.message.bus.service.domain.MessageNote;
import com.sunyo.wlpt.message.bus.service.domain.XmlData;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author 子诚
* Description:
... ... @@ -31,9 +34,8 @@ public interface MessageNoteService {
* 新增,选择性
* @param record the record
* @return insert count
* @throws Exception 异常
*/
int insertSelective(MessageNote record) throws Exception;
int insertSelective(MessageNote record) throws IOException, TimeoutException;
/**
* 查询,根据主键
... ...
... ... @@ -15,7 +15,9 @@ import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeoutException;
/**
* @author 子诚
... ... @@ -89,7 +91,7 @@ public class MessageNoteServiceImpl implements MessageNoteService {
}
@Override
public int insertSelective(MessageNote record) throws Exception
public int insertSelective(MessageNote record) throws IOException, TimeoutException
{
return validateNoteAndFill(record);
}
... ... @@ -237,7 +239,7 @@ public class MessageNoteServiceImpl implements MessageNoteService {
* @param messageNote {@link MessageNote}
* @return 发送信息记录的条数
*/
public int validateNoteAndFill(MessageNote messageNote) throws Exception
public int validateNoteAndFill(MessageNote messageNote) throws IOException, TimeoutException
{
if (validateNote(messageNote)) {
// 填充,服务器名称
... ...
... ... @@ -102,6 +102,10 @@ public class UserInfoServiceImpl implements UserInfoService {
{
String password = userInfo.getPassword();
String virtualHostId = userInfo.getVirtualHostId();
if (StringUtil.isNullOrEmpty(virtualHostId)) {
return new ResultJson<>("400", "用户关系不能为空,情务必选择!");
}
String splitItem = ",";
if (virtualHostId.contains(splitItem)) {
int index = 0;
... ... @@ -312,6 +316,18 @@ public class UserInfoServiceImpl implements UserInfoService {
@Override
public ResultJson deleteUserRelation(UserInfo userInfo) throws IOException, URISyntaxException
{
String virtualHostName = userInfo.getVirtualHostName();
String userInfoId = userInfo.getId();
String splitItem = ",";
if (virtualHostName.contains(splitItem)) {
String[] split = virtualHostName.split(splitItem);
for (int i = 0; i < split.length; i++) {
}
}
int num = userInfoMapper.deleteByPrimaryKey(userInfo.getId());
BusServer busServer = busServerService.selectByPrimaryKey(userInfo.getServerId());
ClientUtils.clearPermissions(busServer, userInfo.getVirtualHostName(), userInfo.getUsername());
... ...
... ... @@ -49,10 +49,10 @@
</delete>
<delete id="deleteByHostName" parameterType="java.lang.String">
delete
from user_info
where virtual_host_name = #{virtualHostName,jdbcType=VARCHAR}
</delete>
delete
from user_info
where virtual_host_name = #{virtualHostName,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="com.sunyo.wlpt.message.bus.service.domain.UserInfo">
... ... @@ -226,8 +226,17 @@
</select>
<select id="selectUserInfoList" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
select group_concat(id) as id,
username,
`password`,
server_id,
`server_name`,
group_concat(virtual_host_id) as virtual_host_id,
group_concat(virtual_host_name) as virtual_host_name,
real_name,
description,
gmt_create,
gmt_modified
from user_info
<where>
<if test="username != null and username != ''">
... ... @@ -240,7 +249,9 @@
and virtual_host_name = #{virtualHostName,jdbcType=VARCHAR}
</if>
</where>
group by username, server_name, server_id
</select>
<select id="validateUserInfo" resultType="com.sunyo.wlpt.message.bus.service.domain.UserInfo">
select
<include refid="Base_Column_List"/>
... ...