Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Apache RocketMQ
Rocketmq
提交
8c029a0d
R
Rocketmq
项目概览
Apache RocketMQ
/
Rocketmq
上一次同步 大约 3 年
通知
268
Star
16139
Fork
68
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rocketmq
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
8c029a0d
编写于
5月 19, 2019
作者:
O
odbozhou
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
1、Define constant 2、Refactoring pull message by the offset
上级
9707846a
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
83 addition
and
50 deletion
+83
-50
openmessaging/src/main/java/io/openmessaging/rocketmq/MessagingAccessPointImpl.java
...a/io/openmessaging/rocketmq/MessagingAccessPointImpl.java
+7
-6
openmessaging/src/main/java/io/openmessaging/rocketmq/consumer/LocalMessageCache.java
...io/openmessaging/rocketmq/consumer/LocalMessageCache.java
+3
-2
openmessaging/src/main/java/io/openmessaging/rocketmq/consumer/PullConsumerImpl.java
.../io/openmessaging/rocketmq/consumer/PullConsumerImpl.java
+66
-39
openmessaging/src/main/java/io/openmessaging/rocketmq/consumer/PushConsumerImpl.java
.../io/openmessaging/rocketmq/consumer/PushConsumerImpl.java
+1
-1
openmessaging/src/main/java/io/openmessaging/rocketmq/domain/NonStandardKeys.java
...ava/io/openmessaging/rocketmq/domain/NonStandardKeys.java
+4
-0
openmessaging/src/test/java/io/openmessaging/rocketmq/consumer/PullConsumerImplTest.java
...openmessaging/rocketmq/consumer/PullConsumerImplTest.java
+1
-1
openmessaging/src/test/java/io/openmessaging/rocketmq/consumer/PushConsumerImplTest.java
...openmessaging/rocketmq/consumer/PushConsumerImplTest.java
+1
-1
未找到文件。
openmessaging/src/main/java/io/openmessaging/rocketmq/MessagingAccessPointImpl.java
浏览文件 @
8c029a0d
...
...
@@ -25,6 +25,7 @@ import io.openmessaging.producer.Producer;
import
io.openmessaging.producer.TransactionStateCheckListener
;
import
io.openmessaging.rocketmq.consumer.PullConsumerImpl
;
import
io.openmessaging.rocketmq.consumer.PushConsumerImpl
;
import
io.openmessaging.rocketmq.domain.NonStandardKeys
;
import
io.openmessaging.rocketmq.producer.ProducerImpl
;
import
java.util.HashSet
;
import
java.util.Set
;
...
...
@@ -57,12 +58,12 @@ public class MessagingAccessPointImpl implements MessagingAccessPoint {
}
@Override
public
Consumer
createConsumer
()
{
String
consumerId
=
accessPointProperties
.
getString
(
"CONSUMER_ID"
);
String
consumerId
=
accessPointProperties
.
getString
(
NonStandardKeys
.
CONSUMER_ID
);
String
[]
nsStrArr
=
consumerId
.
split
(
"_"
);
if
(
nsStrArr
.
length
<
2
)
{
return
new
PushConsumerImpl
(
accessPointProperties
);
}
if
(
"pull"
.
equals
(
nsStrArr
[
0
]))
{
if
(
NonStandardKeys
.
PULL_CONSUMER
.
equals
(
nsStrArr
[
0
]))
{
return
new
PullConsumerImpl
(
accessPointProperties
);
}
return
new
PushConsumerImpl
(
accessPointProperties
);
...
...
@@ -82,24 +83,24 @@ public class MessagingAccessPointImpl implements MessagingAccessPoint {
@Override
public
void
createNamespace
(
String
nsName
)
{
accessPointProperties
.
put
(
"CONSUMER_ID"
,
nsName
);
accessPointProperties
.
put
(
NonStandardKeys
.
CONSUMER_ID
,
nsName
);
}
@Override
public
void
deleteNamespace
(
String
nsName
)
{
accessPointProperties
.
put
(
"CONSUMER_ID"
,
null
);
accessPointProperties
.
put
(
NonStandardKeys
.
CONSUMER_ID
,
null
);
}
@Override
public
void
switchNamespace
(
String
targetNamespace
)
{
accessPointProperties
.
put
(
"CONSUMER_ID"
,
targetNamespace
);
accessPointProperties
.
put
(
NonStandardKeys
.
CONSUMER_ID
,
targetNamespace
);
}
@Override
public
Set
<
String
>
listNamespaces
()
{
return
new
HashSet
<
String
>()
{
{
add
(
accessPointProperties
.
getString
(
"CONSUMER_ID"
));
add
(
accessPointProperties
.
getString
(
NonStandardKeys
.
CONSUMER_ID
));
}
};
}
...
...
openmessaging/src/main/java/io/openmessaging/rocketmq/consumer/LocalMessageCache.java
浏览文件 @
8c029a0d
...
...
@@ -23,6 +23,7 @@ import io.openmessaging.extension.QueueMetaData;
import
io.openmessaging.rocketmq.config.ClientConfig
;
import
io.openmessaging.rocketmq.config.DefaultQueueMetaData
;
import
io.openmessaging.rocketmq.domain.ConsumeRequest
;
import
io.openmessaging.rocketmq.domain.NonStandardKeys
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
...
...
@@ -103,8 +104,8 @@ class LocalMessageCache implements ServiceLifecycle {
MessageExt
poll
(
final
KeyValue
properties
)
{
int
currentPollTimeout
=
clientConfig
.
getOperationTimeout
();
if
(
properties
.
containsKey
(
"TIMEOUT"
))
{
currentPollTimeout
=
properties
.
getInt
(
"TIMEOUT"
);
if
(
properties
.
containsKey
(
NonStandardKeys
.
TIMEOUT
))
{
currentPollTimeout
=
properties
.
getInt
(
NonStandardKeys
.
TIMEOUT
);
}
return
poll
(
currentPollTimeout
);
}
...
...
openmessaging/src/main/java/io/openmessaging/rocketmq/consumer/PullConsumerImpl.java
浏览文件 @
8c029a0d
...
...
@@ -31,6 +31,7 @@ import io.openmessaging.message.Message;
import
io.openmessaging.rocketmq.config.ClientConfig
;
import
io.openmessaging.rocketmq.domain.BytesMessageImpl
;
import
io.openmessaging.rocketmq.domain.ConsumeRequest
;
import
io.openmessaging.rocketmq.domain.NonStandardKeys
;
import
io.openmessaging.rocketmq.utils.BeanUtils
;
import
io.openmessaging.rocketmq.utils.OMSUtil
;
import
java.util.ArrayList
;
...
...
@@ -55,6 +56,10 @@ import org.apache.rocketmq.remoting.exception.RemotingException;
import
org.apache.rocketmq.remoting.protocol.LanguageCode
;
public
class
PullConsumerImpl
implements
Consumer
{
private
static
final
int
PULL_MAX_NUMS
=
32
;
private
static
final
int
PULL_MIN_NUMS
=
1
;
private
final
DefaultMQPullConsumer
rocketmqPullConsumer
;
private
final
KeyValue
properties
;
private
boolean
started
=
false
;
...
...
@@ -93,7 +98,7 @@ public class PullConsumerImpl implements Consumer {
String
consumerId
=
OMSUtil
.
buildInstanceName
();
this
.
rocketmqPullConsumer
.
setInstanceName
(
consumerId
);
properties
.
put
(
"CONSUMER_ID"
,
consumerId
);
properties
.
put
(
NonStandardKeys
.
CONSUMER_ID
,
consumerId
);
this
.
rocketmqPullConsumer
.
setLanguage
(
LanguageCode
.
OMS
);
...
...
@@ -111,9 +116,9 @@ public class PullConsumerImpl implements Consumer {
long
offset
=
localMessageCache
.
nextPullOffset
(
mq
);
PullResult
pullResult
=
consumer
.
pull
(
mq
,
"*"
,
offset
,
localMessageCache
.
nextPullBatchNums
());
offset
,
localMessageCache
.
nextPullBatchNums
());
ProcessQueue
pq
=
rocketmqPullConsumer
.
getDefaultMQPullConsumerImpl
().
getRebalanceImpl
()
.
getProcessQueueTable
().
get
(
mq
);
.
getProcessQueueTable
().
get
(
mq
);
switch
(
pullResult
.
getPullStatus
())
{
case
FOUND:
if
(
pq
!=
null
)
{
...
...
@@ -229,54 +234,37 @@ public class PullConsumerImpl implements Consumer {
@Override
public
Message
receive
(
long
timeout
)
{
KeyValue
properties
=
new
DefaultKeyValue
();
properties
.
put
(
"TIMEOUT"
,
timeout
);
properties
.
put
(
NonStandardKeys
.
TIMEOUT
,
timeout
);
MessageExt
rmqMsg
=
localMessageCache
.
poll
(
properties
);
return
rmqMsg
==
null
?
null
:
OMSUtil
.
msgConvert
(
rmqMsg
);
}
@Override
public
Message
receive
(
String
queueName
,
int
partitionId
,
long
receiptId
,
long
timeout
)
{
KeyValue
properties
=
new
DefaultKeyValue
();
properties
.
put
(
"QUEUE_NAME"
,
queueName
);
properties
.
put
(
"PARTITION_ID"
,
partitionId
);
properties
.
put
(
"RECEIPT_ID"
,
receiptId
);
properties
.
put
(
"TIMEOUT"
,
timeout
);
MessageExt
rmqMsg
=
localMessageCache
.
poll
(
properties
);
return
rmqMsg
==
null
?
null
:
OMSUtil
.
msgConvert
(
rmqMsg
);
}
@Override
public
List
<
Message
>
batchReceive
(
long
timeout
)
{
KeyValue
properties
=
new
DefaultKeyValue
();
properties
.
put
(
"TIMEOUT"
,
timeout
);
List
<
MessageExt
>
rmqMsgs
=
localMessageCache
.
batchPoll
(
properties
);
if
(
null
!=
rmqMsgs
&&
!
rmqMsgs
.
isEmpty
())
{
List
<
Message
>
messages
=
new
ArrayList
<>(
rmqMsgs
.
size
());
for
(
MessageExt
messageExt
:
rmqMsgs
)
{
BytesMessageImpl
bytesMessage
=
OMSUtil
.
msgConvert
(
messageExt
);
messages
.
add
(
bytesMessage
);
MessageQueue
mq
=
null
;
mq
=
getQueue
(
queueName
,
partitionId
,
mq
);
PullResult
pullResult
=
getResult
(
receiptId
,
timeout
,
mq
,
PULL_MIN_NUMS
);
if
(
pullResult
==
null
)
return
null
;
PullStatus
pullStatus
=
pullResult
.
getPullStatus
();
List
<
Message
>
messages
=
new
ArrayList
<>(
16
);
if
(
PullStatus
.
FOUND
.
equals
(
pullStatus
))
{
List
<
MessageExt
>
rmqMsgs
=
pullResult
.
getMsgFoundList
();
if
(
null
!=
rmqMsgs
&&
!
rmqMsgs
.
isEmpty
())
{
for
(
MessageExt
messageExt
:
rmqMsgs
)
{
BytesMessageImpl
bytesMessage
=
OMSUtil
.
msgConvert
(
messageExt
);
messages
.
add
(
bytesMessage
);
}
return
messages
.
get
(
0
);
}
return
messages
;
}
return
null
;
}
@Override
public
List
<
Message
>
batchReceive
(
String
queueName
,
int
partitionId
,
long
receiptId
,
long
timeout
)
{
MessageQueue
mq
=
null
;
try
{
Set
<
MessageQueue
>
messageQueues
=
rocketmqPullConsumer
.
fetchSubscribeMessageQueues
(
queueName
);
for
(
MessageQueue
messageQueue
:
messageQueues
)
{
if
(
messageQueue
.
getQueueId
()
==
partitionId
)
{
mq
=
messageQueue
;
}
}
}
catch
(
MQClientException
e
)
{
e
.
printStackTrace
();
}
private
PullResult
getResult
(
long
receiptId
,
long
timeout
,
MessageQueue
mq
,
int
nums
)
{
PullResult
pullResult
;
try
{
pullResult
=
rocketmqPullConsumer
.
pull
(
mq
,
"*"
,
receiptId
,
4
*
1024
*
1024
,
timeout
);
pullResult
=
rocketmqPullConsumer
.
pull
(
mq
,
"*"
,
receiptId
,
nums
,
timeout
);
}
catch
(
MQClientException
e
)
{
log
.
error
(
"A error occurred when pull message."
,
e
);
return
null
;
...
...
@@ -293,6 +281,46 @@ public class PullConsumerImpl implements Consumer {
if
(
null
==
pullResult
)
{
return
null
;
}
return
pullResult
;
}
private
MessageQueue
getQueue
(
String
queueName
,
int
partitionId
,
MessageQueue
mq
)
{
try
{
Set
<
MessageQueue
>
messageQueues
=
rocketmqPullConsumer
.
fetchSubscribeMessageQueues
(
queueName
);
for
(
MessageQueue
messageQueue
:
messageQueues
)
{
if
(
messageQueue
.
getQueueId
()
==
partitionId
)
{
mq
=
messageQueue
;
}
}
}
catch
(
MQClientException
e
)
{
log
.
error
(
"A error occurred when batch pull message."
,
e
);
}
return
mq
;
}
@Override
public
List
<
Message
>
batchReceive
(
long
timeout
)
{
KeyValue
properties
=
new
DefaultKeyValue
();
properties
.
put
(
NonStandardKeys
.
TIMEOUT
,
timeout
);
List
<
MessageExt
>
rmqMsgs
=
localMessageCache
.
batchPoll
(
properties
);
if
(
null
!=
rmqMsgs
&&
!
rmqMsgs
.
isEmpty
())
{
List
<
Message
>
messages
=
new
ArrayList
<>(
rmqMsgs
.
size
());
for
(
MessageExt
messageExt
:
rmqMsgs
)
{
BytesMessageImpl
bytesMessage
=
OMSUtil
.
msgConvert
(
messageExt
);
messages
.
add
(
bytesMessage
);
}
return
messages
;
}
return
null
;
}
@Override
public
List
<
Message
>
batchReceive
(
String
queueName
,
int
partitionId
,
long
receiptId
,
long
timeout
)
{
MessageQueue
mq
=
null
;
mq
=
getQueue
(
queueName
,
partitionId
,
mq
);
PullResult
pullResult
=
getResult
(
receiptId
,
timeout
,
mq
,
PULL_MAX_NUMS
);
if
(
pullResult
==
null
)
return
null
;
PullStatus
pullStatus
=
pullResult
.
getPullStatus
();
List
<
Message
>
messages
=
new
ArrayList
<>(
16
);
if
(
PullStatus
.
FOUND
.
equals
(
pullStatus
))
{
...
...
@@ -335,7 +363,6 @@ public class PullConsumerImpl implements Consumer {
}
}
this
.
started
=
true
;
currentState
=
ServiceLifeState
.
STARTED
;
}
@Override
...
...
openmessaging/src/main/java/io/openmessaging/rocketmq/consumer/PushConsumerImpl.java
浏览文件 @
8c029a0d
...
...
@@ -91,7 +91,7 @@ public class PushConsumerImpl implements Consumer {
String
consumerId
=
OMSUtil
.
buildInstanceName
();
this
.
rocketmqPushConsumer
.
setInstanceName
(
consumerId
);
properties
.
put
(
"CONSUMER_ID"
,
consumerId
);
properties
.
put
(
NonStandardKeys
.
CONSUMER_ID
,
consumerId
);
this
.
rocketmqPushConsumer
.
setLanguage
(
LanguageCode
.
OMS
);
this
.
rocketmqPushConsumer
.
registerMessageListener
(
new
MessageListenerImpl
());
...
...
openmessaging/src/main/java/io/openmessaging/rocketmq/domain/NonStandardKeys.java
浏览文件 @
8c029a0d
...
...
@@ -29,4 +29,8 @@ public interface NonStandardKeys {
String
PULL_MESSAGE_CACHE_CAPACITY
=
"rmq.pull.message.cache.capacity"
;
String
PRODUCER_ID
=
"PRODUCER_ID"
;
String
CONSUMER_ID
=
"CONSUMER_ID"
;
String
TIMEOUT
=
"TIMEOUT"
;
String
PULL_CONSUMER
=
"PULL"
;
String
PUSH_CONSUMER
=
"PUSH"
;
}
openmessaging/src/test/java/io/openmessaging/rocketmq/consumer/PullConsumerImplTest.java
浏览文件 @
8c029a0d
...
...
@@ -52,7 +52,7 @@ public class PullConsumerImplTest {
final
MessagingAccessPoint
messagingAccessPoint
=
OMS
.
getMessagingAccessPoint
(
"oms:rocketmq://IP1:9876,IP2:9876/namespace"
);
final
ResourceManager
resourceManager
=
messagingAccessPoint
.
resourceManager
();
resourceManager
.
createNamespace
(
"pull
_TestGroup"
);
resourceManager
.
createNamespace
(
NonStandardKeys
.
PULL_CONSUMER
+
"
_TestGroup"
);
consumer
=
messagingAccessPoint
.
createConsumer
();
consumer
.
bindQueue
(
queueName
);
...
...
openmessaging/src/test/java/io/openmessaging/rocketmq/consumer/PushConsumerImplTest.java
浏览文件 @
8c029a0d
...
...
@@ -49,7 +49,7 @@ public class PushConsumerImplTest {
final
MessagingAccessPoint
messagingAccessPoint
=
OMS
.
getMessagingAccessPoint
(
"oms:rocketmq://IP1:9876,IP2:9876/namespace"
);
final
ResourceManager
resourceManager
=
messagingAccessPoint
.
resourceManager
();
resourceManager
.
createNamespace
(
"push
_TestGroup"
);
resourceManager
.
createNamespace
(
NonStandardKeys
.
PUSH_CONSUMER
+
"
_TestGroup"
);
consumer
=
messagingAccessPoint
.
createConsumer
();
Field
field
=
PushConsumerImpl
.
class
.
getDeclaredField
(
"rocketmqPushConsumer"
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录