Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
提交反馈
为 GitLab 提交贡献
登录
切换导航
I
ibzdst
项目
项目
详情
动态
版本
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
议题
1
议题
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
ibiz4jteam
ibzdst
提交
ca7c5ae7
提交
ca7c5ae7
编写于
8月 10, 2022
作者:
fengmin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
mq消息订阅实现
上级
ae81b32f
变更
5
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
224 行增加
和
13 行删除
+224
-13
GeneralConsumeMsgListenerProcessor.java
...e/extensions/util/GeneralConsumeMsgListenerProcessor.java
+53
-0
RuleEngineMapper.java
...in/java/cn/ibizlab/core/rule/mapper/RuleEngineMapper.java
+18
-13
RocktcermqCustmerRunner.java
.../cn/ibizlab/core/util/config/RocktcermqCustmerRunner.java
+93
-0
SpringContextUtil.java
...n/java/cn/ibizlab/core/util/config/SpringContextUtil.java
+57
-0
RuleEngineMapper.xml
...ain/resources/mapper/rule/ruleengine/RuleEngineMapper.xml
+3
-0
未找到文件。
ibzdst-core/src/main/java/cn/ibizlab/core/extensions/util/GeneralConsumeMsgListenerProcessor.java
0 → 100644
浏览文件 @
ca7c5ae7
package
cn
.
ibizlab
.
core
.
extensions
.
util
;
import
cn.ibizlab.core.extensions.domain.EngineMQMsg
;
import
cn.ibizlab.core.extensions.service.RuleEngineExService
;
import
cn.ibizlab.core.util.config.SpringContextUtil
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.TypeReference
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext
;
import
org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus
;
import
org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly
;
import
org.apache.rocketmq.common.message.MessageExt
;
import
org.springframework.util.CollectionUtils
;
import
java.util.List
;
/**
* 通用MQ订阅消息处理
*/
@Slf4j
public
class
GeneralConsumeMsgListenerProcessor
implements
MessageListenerOrderly
{
private
static
RuleEngineExService
ruleEngineExService
=
SpringContextUtil
.
getBean
(
RuleEngineExService
.
class
);
private
String
key
=
""
;
//标记消费者,与需要操作的规则引擎id相等
@Override
public
ConsumeOrderlyStatus
consumeMessage
(
List
<
MessageExt
>
list
,
ConsumeOrderlyContext
consumeOrderlyContext
)
{
if
(
CollectionUtils
.
isEmpty
(
list
))
{
log
.
info
(
"MQ接收消息为空,直接返回成功"
);
return
ConsumeOrderlyStatus
.
SUCCESS
;
}
MessageExt
messageExt
=
list
.
get
(
0
);
try
{
String
topic
=
messageExt
.
getTopic
();
String
tags
=
messageExt
.
getTags
();
String
body
=
new
String
(
messageExt
.
getBody
(),
"utf-8"
);
EngineMQMsg
engineMQMsg
=
JSON
.
parseObject
(
body
,
new
TypeReference
<
EngineMQMsg
>()
{
});
engineMQMsg
.
setEngineId
(
key
);
log
.
info
(
"MQ消息topic={}, tags={}, 消息内容={}"
,
topic
,
tags
,
body
);
ruleEngineExService
.
processData
(
engineMQMsg
);
log
.
info
(
"消费成功"
);
}
catch
(
Exception
e
)
{
log
.
error
(
"获取MQ消息内容异常{}"
,
e
);
}
return
ConsumeOrderlyStatus
.
SUCCESS
;
}
public
GeneralConsumeMsgListenerProcessor
(
String
key
)
{
this
.
key
=
key
;
}
}
ibzdst-core/src/main/java/cn/ibizlab/core/rule/mapper/RuleEngineMapper.java
浏览文件 @
ca7c5ae7
package
cn
.
ibizlab
.
core
.
rule
.
mapper
;
package
cn
.
ibizlab
.
core
.
rule
.
mapper
;
import
java.util.List
;
import
org.apache.ibatis.annotations.*
;
import
java.util.Map
;
import
com.baomidou.mybatisplus.core.mapper.BaseMapper
;
import
com.baomidou.mybatisplus.extension.plugins.pagination.Page
;
import
com.baomidou.mybatisplus.core.metadata.IPage
;
import
com.baomidou.mybatisplus.core.conditions.Wrapper
;
import
java.util.Map
;
import
org.apache.ibatis.annotations.Select
;
import
cn.ibizlab.core.rule.domain.RuleEngine
;
import
cn.ibizlab.core.rule.domain.RuleEngine
;
import
cn.ibizlab.core.rule.filter.RuleEngineSearchContext
;
import
cn.ibizlab.core.rule.filter.RuleEngineSearchContext
;
import
org.springframework.cache.annotation.CacheEvict
;
import
org.springframework.cache.annotation.Cacheable
;
import
java.io.Serializable
;
import
com.baomidou.mybatisplus.core.toolkit.Constants
;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONObject
;
import
com.baomidou.mybatisplus.core.conditions.Wrapper
;
import
com.baomidou.mybatisplus.core.mapper.BaseMapper
;
import
com.baomidou.mybatisplus.core.metadata.IPage
;
import
com.baomidou.mybatisplus.core.toolkit.Constants
;
import
com.baomidou.mybatisplus.extension.plugins.pagination.Page
;
import
org.apache.ibatis.annotations.*
;
import
java.io.Serializable
;
import
java.util.List
;
import
java.util.Map
;
public
interface
RuleEngineMapper
extends
BaseMapper
<
RuleEngine
>
{
public
interface
RuleEngineMapper
extends
BaseMapper
<
RuleEngine
>
{
...
@@ -64,4 +61,12 @@ public interface RuleEngineMapper extends BaseMapper<RuleEngine> {
...
@@ -64,4 +61,12 @@ public interface RuleEngineMapper extends BaseMapper<RuleEngine> {
List
<
RuleEngine
>
selectByModelId
(
@Param
(
"id"
)
Serializable
id
);
List
<
RuleEngine
>
selectByModelId
(
@Param
(
"id"
)
Serializable
id
);
/**
*
* 查询所有消费者信息
*/
List
<
RuleEngine
>
selectCustomerList
();
}
}
ibzdst-core/src/main/java/cn/ibizlab/core/util/config/RocktcermqCustmerRunner.java
0 → 100644
浏览文件 @
ca7c5ae7
package
cn
.
ibizlab
.
core
.
util
.
config
;
import
cn.ibizlab.core.extensions.util.GeneralConsumeMsgListenerProcessor
;
import
cn.ibizlab.core.rule.domain.RuleEngine
;
import
cn.ibizlab.core.rule.mapper.RuleEngineMapper
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONArray
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.rocketmq.client.consumer.DefaultMQPushConsumer
;
import
org.apache.rocketmq.client.exception.MQClientException
;
import
org.apache.rocketmq.common.consumer.ConsumeFromWhere
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.ApplicationArguments
;
import
org.springframework.boot.ApplicationRunner
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.CollectionUtils
;
import
org.springframework.util.StringUtils
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
@Component
@Slf4j
public
class
RocktcermqCustmerRunner
implements
ApplicationRunner
{
@Autowired
private
RuleEngineMapper
ruleEngineMapper
;
@Value
(
"${rocketmq.consumer.namesrvAddr:}"
)
private
String
namesrvAddr
;
//mq消费者
public
static
Map
<
String
,
DefaultMQPushConsumer
>
dataMap
=
new
ConcurrentHashMap
();
@Override
public
void
run
(
ApplicationArguments
args
)
{
List
<
RuleEngine
>
ruleEngines
=
ruleEngineMapper
.
selectCustomerList
();
if
(!
StringUtils
.
isEmpty
(
ruleEngines
))
{
for
(
int
i
=
0
,
len
=
ruleEngines
.
size
();
i
<
len
;
i
++)
{
int
finalI
=
i
;
if
(
isAutoCreate
(
ruleEngines
.
get
(
finalI
).
getExtParams
()))
{
log
.
info
(
"ruleEngineConsumer 正在创建---------------------------------------"
);
DefaultMQPushConsumer
consumer
=
new
DefaultMQPushConsumer
(
ruleEngines
.
get
(
finalI
).
getEngineId
());
consumer
.
setNamesrvAddr
(
namesrvAddr
);
consumer
.
setConsumeThreadMin
(
1
);
consumer
.
setConsumeThreadMax
(
1
);
consumer
.
setConsumeMessageBatchMaxSize
(
1
);
// 设置监听
GeneralConsumeMsgListenerProcessor
messageListener
=
new
GeneralConsumeMsgListenerProcessor
(
ruleEngines
.
get
(
finalI
).
getEngineId
());
consumer
.
registerMessageListener
(
messageListener
);
consumer
.
setConsumeFromWhere
(
ConsumeFromWhere
.
CONSUME_FROM_FIRST_OFFSET
);
consumer
.
setInstanceName
(
"dstMQMsgConsumer"
);
try
{
consumer
.
subscribe
(
ruleEngines
.
get
(
finalI
).
getModelId
(),
"*"
);
consumer
.
start
();
if
(
dataMap
.
get
(
ruleEngines
.
get
(
finalI
))
==
null
)
{
dataMap
.
put
(
ruleEngines
.
get
(
finalI
).
getEngineId
(),
consumer
);
log
.
info
(
"ruleEngineConsumer 创建成功 groupName={}, topics={}, namesrvAddr={}"
,
ruleEngines
.
get
(
finalI
).
getEngineId
(),
ruleEngines
.
get
(
finalI
).
getModelId
(),
namesrvAddr
);
}
else
{
log
.
warn
(
"ruleEngineConsumer 不能重复创建 groupName={}, topics={}, namesrvAddr={}"
,
ruleEngines
.
get
(
finalI
).
getEngineId
(),
ruleEngines
.
get
(
finalI
).
getModelId
(),
namesrvAddr
);
}
}
catch
(
MQClientException
e
)
{
e
.
printStackTrace
();
log
.
error
(
"ruleEngineConsumer 创建失败!"
);
}
}
}
}
}
/**
* 判断是否自动创建
*
* @param extParams
* @return
*/
public
boolean
isAutoCreate
(
String
extParams
)
{
JSONArray
objects
=
JSON
.
parseArray
(
extParams
);
boolean
flat
=
true
;
if
(!
CollectionUtils
.
isEmpty
(
objects
))
{
for
(
int
j
=
0
,
len1
=
objects
.
size
();
j
<
len1
;
j
++)
{
String
property
=
objects
.
getJSONObject
(
j
).
getString
(
"property"
);
if
(
"isAuto"
.
equals
(
property
))
{
if
(
"N"
.
equals
(
objects
.
getJSONObject
(
j
).
getString
(
"value"
)))
{
flat
=
false
;
}
break
;
}
}
}
return
flat
;
}
}
ibzdst-core/src/main/java/cn/ibizlab/core/util/config/SpringContextUtil.java
0 → 100644
浏览文件 @
ca7c5ae7
package
cn
.
ibizlab
.
core
.
util
.
config
;
import
org.springframework.beans.BeansException
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationContextAware
;
import
org.springframework.stereotype.Component
;
import
java.util.Optional
;
@Component
public
class
SpringContextUtil
implements
ApplicationContextAware
{
private
static
ApplicationContext
applicationContext
;
@Override
public
void
setApplicationContext
(
ApplicationContext
applicationContext
)
throws
BeansException
{
SpringContextUtil
.
applicationContext
=
applicationContext
;
}
/**
* @Description: 获取spring容器中的bean,通过bean名称获取
* @param beanName bean名称
* @return: Object 返回Object,需要做强制类型转换
*/
public
static
Object
getBean
(
String
beanName
){
return
applicationContext
.
getBean
(
beanName
);
}
/**
* @Description: 获取spring容器中的bean, 通过bean类型获取
* @param beanClass bean 类型
* @return: T 返回指定类型的bean实例
*/
public
static
<
T
>
T
getBean
(
Class
<
T
>
beanClass
)
{
return
applicationContext
.
getBean
(
beanClass
);
}
public
static
<
T
>
Optional
<
T
>
getBeanOptional
(
Class
<
T
>
beanClass
)
{
try
{
T
bean
=
applicationContext
.
getBean
(
beanClass
);
return
Optional
.
of
(
bean
);
}
catch
(
Exception
e
)
{
return
Optional
.
empty
();
}
}
/**
* @Description: 获取spring容器中的bean, 通过bean名称和bean类型精确获取
* @param beanName bean 名称
* @param beanClass bean 类型
* @return: T 返回指定类型的bean实例
*/
public
static
<
T
>
T
getBean
(
String
beanName
,
Class
<
T
>
beanClass
){
return
applicationContext
.
getBean
(
beanName
,
beanClass
);
}
}
ibzdst-core/src/main/resources/mapper/rule/ruleengine/RuleEngineMapper.xml
浏览文件 @
ca7c5ae7
...
@@ -41,6 +41,9 @@
...
@@ -41,6 +41,9 @@
<where><if
test=
"ew!=null and ew.sqlSegment!=null and !ew.emptyOfWhere"
>
${ew.sqlSegment}
</if></where>
<where><if
test=
"ew!=null and ew.sqlSegment!=null and !ew.emptyOfWhere"
>
${ew.sqlSegment}
</if></where>
<if
test=
"ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere"
>
${ew.sqlSegment}
</if>
<if
test=
"ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere"
>
${ew.sqlSegment}
</if>
</select>
</select>
<select
id=
"selectCustomerList"
resultType=
"cn.ibizlab.core.rule.domain.RuleEngine"
>
SELECT ENGINEID ,(SELECT CODENAME FROM IBZMODEL WHERE E.MODELID=MODELID) AS MODELID,EXTPARAMS FROM IBZRULEENGINE E WHERE MODELID IS NOT NULL
</select>
<!--数据查询[Default]-->
<!--数据查询[Default]-->
<sql
id=
"Default"
databaseId=
"mysql"
>
<sql
id=
"Default"
databaseId=
"mysql"
>
...
...
编辑
预览
Markdown
格式
0%
请重试
or
添加新附件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录