Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Wheat_Liu
Rocketmq
提交
61a0fcba
R
Rocketmq
项目概览
Wheat_Liu
/
Rocketmq
与 Fork 源项目一致
Fork自
Apache RocketMQ / Rocketmq
通知
1
Star
0
Fork
0
代码
文件
提交
分支
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
61a0fcba
编写于
8月 08, 2018
作者:
D
duhengforever
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Reslove compatibility issues and keep consistent with the old API
上级
7cae5839
变更
13
显示空白变更内容
内联
并排
Showing
13 changed file
with
192 addition
and
128 deletion
+192
-128
client/src/main/java/org/apache/rocketmq/client/impl/producer/DefaultMQProducerImpl.java
.../rocketmq/client/impl/producer/DefaultMQProducerImpl.java
+31
-29
client/src/main/java/org/apache/rocketmq/client/impl/producer/MQProducerInner.java
...apache/rocketmq/client/impl/producer/MQProducerInner.java
+3
-4
client/src/main/java/org/apache/rocketmq/client/producer/DefaultMQProducer.java
...rg/apache/rocketmq/client/producer/DefaultMQProducer.java
+3
-1
client/src/main/java/org/apache/rocketmq/client/producer/LocalTransactionExecuter.java
...he/rocketmq/client/producer/LocalTransactionExecuter.java
+23
-0
client/src/main/java/org/apache/rocketmq/client/producer/MQProducer.java
.../java/org/apache/rocketmq/client/producer/MQProducer.java
+2
-1
client/src/main/java/org/apache/rocketmq/client/producer/TransactionCheckListener.java
...he/rocketmq/client/producer/TransactionCheckListener.java
+2
-19
client/src/main/java/org/apache/rocketmq/client/producer/TransactionMQProducer.java
...pache/rocketmq/client/producer/TransactionMQProducer.java
+38
-11
common/src/main/java/org/apache/rocketmq/common/BrokerConfig.java
...rc/main/java/org/apache/rocketmq/common/BrokerConfig.java
+2
-2
common/src/main/java/org/apache/rocketmq/common/MixAll.java
common/src/main/java/org/apache/rocketmq/common/MixAll.java
+1
-1
example/src/main/java/org/apache/rocketmq/example/benchmark/TransactionProducer.java
...pache/rocketmq/example/benchmark/TransactionProducer.java
+46
-37
example/src/main/java/org/apache/rocketmq/example/transaction/LocalTransactionExecuterImpl.java
...tmq/example/transaction/LocalTransactionExecuterImpl.java
+27
-0
example/src/main/java/org/apache/rocketmq/example/transaction/TransactionCheckListenerImpl.java
...tmq/example/transaction/TransactionCheckListenerImpl.java
+8
-18
example/src/main/java/org/apache/rocketmq/example/transaction/TransactionProducer.java
...che/rocketmq/example/transaction/TransactionProducer.java
+6
-5
未找到文件。
client/src/main/java/org/apache/rocketmq/client/impl/producer/DefaultMQProducerImpl.java
浏览文件 @
61a0fcba
...
...
@@ -16,6 +16,22 @@
*/
package
org.apache.rocketmq.client.impl.producer
;
import
java.io.IOException
;
import
java.net.UnknownHostException
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Random
;
import
java.util.Set
;
import
java.util.concurrent.BlockingQueue
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ConcurrentMap
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.LinkedBlockingQueue
;
import
java.util.concurrent.RejectedExecutionException
;
import
java.util.concurrent.ThreadPoolExecutor
;
import
java.util.concurrent.TimeUnit
;
import
org.apache.rocketmq.client.QueryResult
;
import
org.apache.rocketmq.client.Validators
;
import
org.apache.rocketmq.client.common.ClientErrorCode
;
...
...
@@ -31,12 +47,13 @@ import org.apache.rocketmq.client.impl.factory.MQClientInstance;
import
org.apache.rocketmq.client.latency.MQFaultStrategy
;
import
org.apache.rocketmq.client.log.ClientLogger
;
import
org.apache.rocketmq.client.producer.DefaultMQProducer
;
import
org.apache.rocketmq.client.producer.LocalTransactionExecuter
;
import
org.apache.rocketmq.client.producer.LocalTransactionState
;
import
org.apache.rocketmq.client.producer.MessageQueueSelector
;
import
org.apache.rocketmq.client.producer.SendCallback
;
import
org.apache.rocketmq.client.producer.SendResult
;
import
org.apache.rocketmq.client.producer.SendStatus
;
import
org.apache.rocketmq.client.producer.TransactionListener
;
import
org.apache.rocketmq.client.producer.Transaction
Check
Listener
;
import
org.apache.rocketmq.client.producer.TransactionMQProducer
;
import
org.apache.rocketmq.client.producer.TransactionSendResult
;
import
org.apache.rocketmq.common.MixAll
;
...
...
@@ -65,23 +82,6 @@ import org.apache.rocketmq.remoting.exception.RemotingConnectException;
import
org.apache.rocketmq.remoting.exception.RemotingException
;
import
org.apache.rocketmq.remoting.exception.RemotingTimeoutException
;
import
org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException
;
import
java.util.concurrent.RejectedExecutionException
;
import
java.io.IOException
;
import
java.net.UnknownHostException
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Random
;
import
java.util.Set
;
import
java.util.concurrent.BlockingQueue
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ConcurrentMap
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.LinkedBlockingQueue
;
import
java.util.concurrent.ThreadPoolExecutor
;
import
java.util.concurrent.TimeUnit
;
public
class
DefaultMQProducerImpl
implements
MQProducerInner
{
private
final
InternalLogger
log
=
ClientLogger
.
getLog
();
...
...
@@ -122,8 +122,8 @@ public class DefaultMQProducerImpl implements MQProducerInner {
}
else
{
this
.
checkRequestQueue
=
new
LinkedBlockingQueue
<
Runnable
>(
2000
);
this
.
checkExecutor
=
new
ThreadPoolExecutor
(
1
,
1
,
producer
.
getCheckThreadPoolMinSize
()
,
producer
.
getCheckThreadPoolMaxSize
()
,
1000
*
60
,
TimeUnit
.
MILLISECONDS
,
this
.
checkRequestQueue
);
...
...
@@ -131,8 +131,9 @@ public class DefaultMQProducerImpl implements MQProducerInner {
}
public
void
destroyTransactionEnv
()
{
if
(
this
.
checkExecutor
!=
null
)
{
this
.
checkExecutor
.
shutdown
();
this
.
checkRequestQueue
.
clear
();
}
}
public
void
registerSendMessageHook
(
final
SendMessageHook
hook
)
{
...
...
@@ -244,16 +245,17 @@ public class DefaultMQProducerImpl implements MQProducerInner {
}
@Override
public
TransactionListener
checkListener
()
{
public
Transaction
Check
Listener
checkListener
()
{
if
(
this
.
defaultMQProducer
instanceof
TransactionMQProducer
)
{
TransactionMQProducer
producer
=
(
TransactionMQProducer
)
defaultMQProducer
;
return
producer
.
getTransactionListener
();
return
producer
.
getTransaction
Check
Listener
();
}
return
null
;
}
@Override
public
void
checkTransactionState
(
final
String
addr
,
final
MessageExt
msg
,
final
CheckTransactionStateRequestHeader
header
)
{
Runnable
request
=
new
Runnable
()
{
...
...
@@ -264,12 +266,12 @@ public class DefaultMQProducerImpl implements MQProducerInner {
@Override
public
void
run
()
{
TransactionListener
transactionCheckListener
=
DefaultMQProducerImpl
.
this
.
checkListener
();
Transaction
Check
Listener
transactionCheckListener
=
DefaultMQProducerImpl
.
this
.
checkListener
();
if
(
transactionCheckListener
!=
null
)
{
LocalTransactionState
localTransactionState
=
LocalTransactionState
.
UNKNOW
;
Throwable
exception
=
null
;
try
{
localTransactionState
=
transactionCheckListener
.
checkLocalTransaction
(
message
);
localTransactionState
=
transactionCheckListener
.
checkLocalTransaction
State
(
message
);
}
catch
(
Throwable
e
)
{
log
.
error
(
"Broker call checkTransactionState, but checkLocalTransactionState exception"
,
e
);
exception
=
e
;
...
...
@@ -1096,9 +1098,9 @@ public class DefaultMQProducerImpl implements MQProducerInner {
}
public
TransactionSendResult
sendMessageInTransaction
(
final
Message
msg
,
final
TransactionListener
tra
nExecuter
,
final
Object
arg
)
final
LocalTransactionExecuter
localTransactio
nExecuter
,
final
Object
arg
)
throws
MQClientException
{
if
(
null
==
tra
nExecuter
)
{
if
(
null
==
localTransactio
nExecuter
)
{
throw
new
MQClientException
(
"tranExecutor is null"
,
null
);
}
Validators
.
checkMessage
(
msg
,
this
.
defaultMQProducer
);
...
...
@@ -1124,7 +1126,7 @@ public class DefaultMQProducerImpl implements MQProducerInner {
if
(
null
!=
transactionId
&&
!
""
.
equals
(
transactionId
))
{
msg
.
setTransactionId
(
transactionId
);
}
localTransactionState
=
tranExecuter
.
executeLocalTransaction
(
msg
,
arg
);
localTransactionState
=
localTransactionExecuter
.
executeLocalTransactionBranch
(
msg
,
arg
);
if
(
null
==
localTransactionState
)
{
localTransactionState
=
LocalTransactionState
.
UNKNOW
;
}
...
...
client/src/main/java/org/apache/rocketmq/client/impl/producer/MQProducerInner.java
浏览文件 @
61a0fcba
...
...
@@ -16,18 +16,17 @@
*/
package
org.apache.rocketmq.client.impl.producer
;
import
org.apache.rocketmq.client.producer.TransactionListener
;
import
java.util.Set
;
import
org.apache.rocketmq.client.producer.TransactionCheckListener
;
import
org.apache.rocketmq.common.message.MessageExt
;
import
org.apache.rocketmq.common.protocol.header.CheckTransactionStateRequestHeader
;
import
java.util.Set
;
public
interface
MQProducerInner
{
Set
<
String
>
getPublishTopicList
();
boolean
isPublishTopicNeedUpdate
(
final
String
topic
);
TransactionListener
checkListener
();
Transaction
Check
Listener
checkListener
();
void
checkTransactionState
(
final
String
addr
,
...
...
client/src/main/java/org/apache/rocketmq/client/producer/DefaultMQProducer.java
浏览文件 @
61a0fcba
...
...
@@ -464,12 +464,14 @@ public class DefaultMQProducer extends ClientConfig implements MQProducer {
* This method is to send transactional messages.
*
* @param msg Transactional message to send.
* @param tranExecuter local transaction executor.
* @param arg Argument used along with local transaction executor.
* @return Transaction result.
* @throws MQClientException if there is any client error.
*/
@Override
public
TransactionSendResult
sendMessageInTransaction
(
Message
msg
,
final
Object
arg
)
public
TransactionSendResult
sendMessageInTransaction
(
Message
msg
,
LocalTransactionExecuter
tranExecuter
,
final
Object
arg
)
throws
MQClientException
{
throw
new
RuntimeException
(
"sendMessageInTransaction not implement, please use TransactionMQProducer class"
);
}
...
...
client/src/main/java/org/apache/rocketmq/client/producer/LocalTransactionExecuter.java
0 → 100644
浏览文件 @
61a0fcba
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.apache.rocketmq.client.producer
;
import
org.apache.rocketmq.common.message.Message
;
public
interface
LocalTransactionExecuter
{
LocalTransactionState
executeLocalTransactionBranch
(
final
Message
msg
,
final
Object
arg
);
}
client/src/main/java/org/apache/rocketmq/client/producer/MQProducer.java
浏览文件 @
61a0fcba
...
...
@@ -80,7 +80,8 @@ public interface MQProducer extends MQAdmin {
void
sendOneway
(
final
Message
msg
,
final
MessageQueueSelector
selector
,
final
Object
arg
)
throws
MQClientException
,
RemotingException
,
InterruptedException
;
TransactionSendResult
sendMessageInTransaction
(
final
Message
msg
,
final
Object
arg
)
throws
MQClientException
;
TransactionSendResult
sendMessageInTransaction
(
final
Message
msg
,
final
LocalTransactionExecuter
tranExecuter
,
final
Object
arg
)
throws
MQClientException
;
//for batch
SendResult
send
(
final
Collection
<
Message
>
msgs
)
throws
MQClientException
,
RemotingException
,
MQBrokerException
,
...
...
client/src/main/java/org/apache/rocketmq/client/producer/TransactionListener.java
→
client/src/main/java/org/apache/rocketmq/client/producer/Transaction
Check
Listener.java
浏览文件 @
61a0fcba
...
...
@@ -16,25 +16,8 @@
*/
package
org.apache.rocketmq.client.producer
;
import
org.apache.rocketmq.common.message.Message
;
import
org.apache.rocketmq.common.message.MessageExt
;
public
interface
TransactionListener
{
/**
* When send transactional prepare(half) message succeed, this method will be invoked to execute local transaction.
*
* @param msg Half(prepare) message
* @param arg Custom business parameter
* @return Transaction state
*/
LocalTransactionState
executeLocalTransaction
(
final
Message
msg
,
final
Object
arg
);
/**
* When no response to prepare(half) message. broker will send check message to check the transaction status, and this
* method will be invoked to get local transaction status.
*
* @param msg Check message
* @return Transaction state
*/
LocalTransactionState
checkLocalTransaction
(
final
MessageExt
msg
);
public
interface
TransactionCheckListener
{
LocalTransactionState
checkLocalTransactionState
(
final
MessageExt
msg
);
}
client/src/main/java/org/apache/rocketmq/client/producer/TransactionMQProducer.java
浏览文件 @
61a0fcba
...
...
@@ -16,14 +16,16 @@
*/
package
org.apache.rocketmq.client.producer
;
import
java.util.concurrent.ExecutorService
;
import
org.apache.rocketmq.client.exception.MQClientException
;
import
org.apache.rocketmq.common.message.Message
;
import
org.apache.rocketmq.remoting.RPCHook
;
import
java.util.concurrent.ExecutorService
;
public
class
TransactionMQProducer
extends
DefaultMQProducer
{
private
TransactionListener
transactionListener
;
private
TransactionCheckListener
transactionCheckListener
;
private
int
checkThreadPoolMinSize
=
1
;
private
int
checkThreadPoolMaxSize
=
1
;
private
int
checkRequestHoldMax
=
2000
;
private
ExecutorService
executorService
;
...
...
@@ -51,20 +53,45 @@ public class TransactionMQProducer extends DefaultMQProducer {
}
@Override
public
TransactionSendResult
sendMessageInTransaction
(
final
Message
msg
,
final
Object
arg
)
throws
MQClientException
{
if
(
null
==
this
.
transactionListener
)
{
throw
new
MQClientException
(
"TransactionListener is null"
,
null
);
public
TransactionSendResult
sendMessageInTransaction
(
final
Message
msg
,
final
LocalTransactionExecuter
tranExecuter
,
final
Object
arg
)
throws
MQClientException
{
if
(
null
==
this
.
transactionCheckListener
)
{
throw
new
MQClientException
(
"localTransactionBranchCheckListener is null"
,
null
);
}
return
this
.
defaultMQProducerImpl
.
sendMessageInTransaction
(
msg
,
tranExecuter
,
arg
);
}
public
TransactionCheckListener
getTransactionCheckListener
()
{
return
transactionCheckListener
;
}
public
void
setTransactionCheckListener
(
TransactionCheckListener
transactionCheckListener
)
{
this
.
transactionCheckListener
=
transactionCheckListener
;
}
public
int
getCheckThreadPoolMinSize
()
{
return
checkThreadPoolMinSize
;
}
public
void
setCheckThreadPoolMinSize
(
int
checkThreadPoolMinSize
)
{
this
.
checkThreadPoolMinSize
=
checkThreadPoolMinSize
;
}
public
int
getCheckThreadPoolMaxSize
()
{
return
checkThreadPoolMaxSize
;
}
return
this
.
defaultMQProducerImpl
.
sendMessageInTransaction
(
msg
,
transactionListener
,
arg
);
public
void
setCheckThreadPoolMaxSize
(
int
checkThreadPoolMaxSize
)
{
this
.
checkThreadPoolMaxSize
=
checkThreadPoolMaxSize
;
}
public
TransactionListener
getTransactionListener
()
{
return
transactionListener
;
public
int
getCheckRequestHoldMax
()
{
return
checkRequestHoldMax
;
}
public
void
set
TransactionListener
(
TransactionListener
transactionListener
)
{
this
.
transactionListener
=
transactionListener
;
public
void
set
CheckRequestHoldMax
(
int
checkRequestHoldMax
)
{
this
.
checkRequestHoldMax
=
checkRequestHoldMax
;
}
public
ExecutorService
getExecutorService
()
{
...
...
common/src/main/java/org/apache/rocketmq/common/BrokerConfig.java
浏览文件 @
61a0fcba
...
...
@@ -150,13 +150,13 @@ public class BrokerConfig {
* that can be checked.
*/
@ImportantField
private
long
transactionTimeOut
=
3
*
1000
;
private
long
transactionTimeOut
=
6
*
1000
;
/**
* The maximum number of times the message was checked, if exceed this value, this message will be discarded.
*/
@ImportantField
private
int
transactionCheckMax
=
5
;
private
int
transactionCheckMax
=
1
5
;
/**
* Transaction message check interval.
...
...
common/src/main/java/org/apache/rocketmq/common/MixAll.java
浏览文件 @
61a0fcba
...
...
@@ -57,7 +57,7 @@ public class MixAll {
public
static
final
String
WS_DOMAIN_SUBGROUP
=
System
.
getProperty
(
"rocketmq.namesrv.domain.subgroup"
,
"nsaddr"
);
//http://jmenv.tbsite.net:8080/rocketmq/nsaddr
//public static final String WS_ADDR = "http://" + WS_DOMAIN_NAME + ":8080/rocketmq/" + WS_DOMAIN_SUBGROUP;
public
static
final
String
AUTO_CREATE_TOPIC_KEY_TOPIC
=
"
AUTO_CREATE_TOPIC_KEY
"
;
// Will be created at broker when isAutoCreateTopicEnable
public
static
final
String
AUTO_CREATE_TOPIC_KEY_TOPIC
=
"
TBW102
"
;
// Will be created at broker when isAutoCreateTopicEnable
public
static
final
String
BENCHMARK_TOPIC
=
"BenchmarkTest"
;
public
static
final
String
DEFAULT_PRODUCER_GROUP
=
"DEFAULT_PRODUCER"
;
public
static
final
String
DEFAULT_CONSUMER_GROUP
=
"DEFAULT_CONSUMER"
;
...
...
example/src/main/java/org/apache/rocketmq/example/benchmark/TransactionProducer.java
浏览文件 @
61a0fcba
...
...
@@ -17,15 +17,6 @@
package
org.apache.rocketmq.example.benchmark
;
import
org.apache.rocketmq.client.exception.MQClientException
;
import
org.apache.rocketmq.client.producer.LocalTransactionState
;
import
org.apache.rocketmq.client.producer.SendResult
;
import
org.apache.rocketmq.client.producer.TransactionListener
;
import
org.apache.rocketmq.client.producer.TransactionMQProducer
;
import
org.apache.rocketmq.common.message.Message
;
import
org.apache.rocketmq.common.message.MessageExt
;
import
org.apache.rocketmq.remoting.common.RemotingHelper
;
import
java.io.UnsupportedEncodingException
;
import
java.util.LinkedList
;
import
java.util.Timer
;
...
...
@@ -33,18 +24,27 @@ import java.util.TimerTask;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.atomic.AtomicLong
;
import
org.apache.rocketmq.client.exception.MQClientException
;
import
org.apache.rocketmq.client.producer.LocalTransactionExecuter
;
import
org.apache.rocketmq.client.producer.LocalTransactionState
;
import
org.apache.rocketmq.client.producer.SendResult
;
import
org.apache.rocketmq.client.producer.TransactionCheckListener
;
import
org.apache.rocketmq.client.producer.TransactionMQProducer
;
import
org.apache.rocketmq.common.message.Message
;
import
org.apache.rocketmq.common.message.MessageExt
;
import
org.apache.rocketmq.remoting.common.RemotingHelper
;
public
class
TransactionProducer
{
private
static
int
threadCount
;
private
static
int
messageSize
;
private
static
boolean
is
C
heck
;
private
static
boolean
is
CheckF
alse
;
private
static
boolean
is
c
heck
;
private
static
boolean
is
checkff
alse
;
public
static
void
main
(
String
[]
args
)
throws
MQClientException
,
UnsupportedEncodingException
{
threadCount
=
args
.
length
>=
1
?
Integer
.
parseInt
(
args
[
0
])
:
32
;
messageSize
=
args
.
length
>=
2
?
Integer
.
parseInt
(
args
[
1
])
:
1024
*
2
;
is
C
heck
=
args
.
length
>=
3
&&
Boolean
.
parseBoolean
(
args
[
2
]);
is
CheckF
alse
=
args
.
length
>=
4
&&
Boolean
.
parseBoolean
(
args
[
3
]);
is
c
heck
=
args
.
length
>=
3
&&
Boolean
.
parseBoolean
(
args
[
2
]);
is
checkff
alse
=
args
.
length
>=
4
&&
Boolean
.
parseBoolean
(
args
[
3
]);
final
Message
msg
=
buildMessage
(
messageSize
);
...
...
@@ -73,8 +73,8 @@ public class TransactionProducer {
Long
[]
end
=
snapshotList
.
getLast
();
final
long
sendTps
=
(
long
)
(((
end
[
3
]
-
begin
[
3
])
/
(
double
)
(
end
[
0
]
-
begin
[
0
]))
*
1000L
);
final
double
averageRT
=
(
end
[
5
]
-
begin
[
5
])
/
(
double
)(
end
[
3
]
-
begin
[
3
]);
(
long
)
(((
end
[
3
]
-
begin
[
3
])
/
(
double
)
(
end
[
0
]
-
begin
[
0
]))
*
1000L
);
final
double
averageRT
=
(
end
[
5
]
-
begin
[
5
])
/
(
double
)
(
end
[
3
]
-
begin
[
3
]);
System
.
out
.
printf
(
"Send TPS: %d Max RT: %d Average RT: %7.3f Send Failed: %d Response Failed: %d transaction checkCount: %d %n"
,
...
...
@@ -92,14 +92,16 @@ public class TransactionProducer {
}
},
10000
,
10000
);
final
Transaction
Listener
transaction
Listener
=
new
Transaction
ListenerImpl
(
isCheckFalse
,
isCheck
,
statsBenchmark
);
final
Transaction
CheckListener
transactionCheck
Listener
=
new
Transaction
CheckListenerBImpl
(
ischeckffalse
,
statsBenchmark
);
final
TransactionMQProducer
producer
=
new
TransactionMQProducer
(
"benchmark_transaction_producer"
);
producer
.
setInstanceName
(
Long
.
toString
(
System
.
currentTimeMillis
()));
producer
.
setTransaction
Listener
(
transaction
Listener
);
producer
.
setTransaction
CheckListener
(
transactionCheck
Listener
);
producer
.
setDefaultTopicQueueNums
(
1000
);
producer
.
start
();
final
TransactionExecuterBImpl
tranExecuter
=
new
TransactionExecuterBImpl
(
ischeck
);
for
(
int
i
=
0
;
i
<
threadCount
;
i
++)
{
sendThreadPool
.
execute
(
new
Runnable
()
{
@Override
...
...
@@ -109,7 +111,7 @@ public class TransactionProducer {
// Thread.sleep(1000);
final
long
beginTimestamp
=
System
.
currentTimeMillis
();
SendResult
sendResult
=
producer
.
sendMessageInTransaction
(
msg
,
null
);
producer
.
sendMessageInTransaction
(
msg
,
tranExecuter
,
null
);
if
(
sendResult
!=
null
)
{
statsBenchmark
.
getSendRequestSuccessCount
().
incrementAndGet
();
statsBenchmark
.
getReceiveResponseSuccessCount
().
incrementAndGet
();
...
...
@@ -122,7 +124,8 @@ public class TransactionProducer {
boolean
updated
=
statsBenchmark
.
getSendMessageMaxRT
().
compareAndSet
(
prevMaxRT
,
currentRT
);
if
(
updated
)
{
break
;
}
if
(
updated
)
break
;
prevMaxRT
=
statsBenchmark
.
getSendMessageMaxRT
().
get
();
}
...
...
@@ -150,37 +153,43 @@ public class TransactionProducer {
}
}
class
TransactionExecuterBImpl
implements
LocalTransactionExecuter
{
class
TransactionListenerImpl
implements
TransactionListener
{
private
boolean
isCheckFalse
;
private
boolean
ischeck
;
public
TransactionExecuterBImpl
(
boolean
ischeck
)
{
this
.
ischeck
=
ischeck
;
}
@Override
public
LocalTransactionState
executeLocalTransactionBranch
(
final
Message
msg
,
final
Object
arg
)
{
if
(
ischeck
)
{
return
LocalTransactionState
.
UNKNOW
;
}
return
LocalTransactionState
.
COMMIT_MESSAGE
;
}
}
class
TransactionCheckListenerBImpl
implements
TransactionCheckListener
{
private
boolean
ischeckffalse
;
private
StatsBenchmarkTProducer
statsBenchmarkTProducer
;
private
boolean
isCheckLocal
;
public
Transaction
ListenerImpl
(
boolean
isCheckFalse
,
boolean
isCheckLocal
,
public
Transaction
CheckListenerBImpl
(
boolean
ischeckffalse
,
StatsBenchmarkTProducer
statsBenchmarkTProducer
)
{
this
.
isCheckFalse
=
isCheckFalse
;
this
.
isCheckLocal
=
isCheckLocal
;
this
.
ischeckffalse
=
ischeckffalse
;
this
.
statsBenchmarkTProducer
=
statsBenchmarkTProducer
;
}
@Override
public
LocalTransactionState
checkLocalTransaction
(
MessageExt
msg
)
{
public
LocalTransactionState
checkLocalTransaction
State
(
MessageExt
msg
)
{
statsBenchmarkTProducer
.
getCheckRequestSuccessCount
().
incrementAndGet
();
if
(
is
CheckF
alse
)
{
if
(
is
checkff
alse
)
{
return
LocalTransactionState
.
ROLLBACK_MESSAGE
;
}
return
LocalTransactionState
.
COMMIT_MESSAGE
;
}
@Override
public
LocalTransactionState
executeLocalTransaction
(
final
Message
msg
,
final
Object
arg
)
{
if
(
isCheckLocal
)
{
return
LocalTransactionState
.
UNKNOW
;
}
return
LocalTransactionState
.
COMMIT_MESSAGE
;
}
}
class
StatsBenchmarkTProducer
{
...
...
example/src/main/java/org/apache/rocketmq/example/transaction/LocalTransactionExecuterImpl.java
0 → 100644
浏览文件 @
61a0fcba
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.apache.rocketmq.example.transaction
;
import
org.apache.rocketmq.client.producer.LocalTransactionExecuter
;
import
org.apache.rocketmq.client.producer.LocalTransactionState
;
import
org.apache.rocketmq.common.message.Message
;
public
class
LocalTransactionExecuterImpl
implements
LocalTransactionExecuter
{
@Override
public
LocalTransactionState
executeLocalTransactionBranch
(
Message
msg
,
Object
arg
)
{
return
LocalTransactionState
.
UNKNOW
;
}
}
example/src/main/java/org/apache/rocketmq/example/transaction/TransactionListenerImpl.java
→
example/src/main/java/org/apache/rocketmq/example/transaction/Transaction
Check
ListenerImpl.java
浏览文件 @
61a0fcba
...
...
@@ -16,30 +16,18 @@
*/
package
org.apache.rocketmq.example.transaction
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
org.apache.rocketmq.client.producer.LocalTransactionState
;
import
org.apache.rocketmq.client.producer.TransactionListener
;
import
org.apache.rocketmq.common.message.Message
;
import
org.apache.rocketmq.client.producer.TransactionCheckListener
;
import
org.apache.rocketmq.common.message.MessageExt
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.atomic.AtomicInteger
;
public
class
TransactionListenerImpl
implements
TransactionListener
{
private
AtomicInteger
transactionIndex
=
new
AtomicInteger
(
0
);
private
ConcurrentHashMap
<
String
,
Integer
>
localTrans
=
new
ConcurrentHashMap
<>();
public
class
TransactionCheckListenerImpl
implements
TransactionCheckListener
{
@Override
public
LocalTransactionState
executeLocalTransaction
(
Message
msg
,
Object
arg
)
{
int
value
=
transactionIndex
.
getAndIncrement
();
int
status
=
value
%
3
;
localTrans
.
put
(
msg
.
getTransactionId
(),
status
);
return
LocalTransactionState
.
UNKNOW
;
}
private
AtomicInteger
localTrans
=
new
AtomicInteger
(
0
);
@Override
public
LocalTransactionState
checkLocalTransaction
(
MessageExt
msg
)
{
Integer
status
=
localTrans
.
get
(
msg
.
getTransactionId
())
;
public
LocalTransactionState
checkLocalTransaction
State
(
MessageExt
msg
)
{
Integer
status
=
localTrans
.
get
AndIncrement
()
%
3
;
if
(
null
!=
status
)
{
switch
(
status
)
{
case
0
:
...
...
@@ -48,6 +36,8 @@ public class TransactionListenerImpl implements TransactionListener {
return
LocalTransactionState
.
COMMIT_MESSAGE
;
case
2
:
return
LocalTransactionState
.
ROLLBACK_MESSAGE
;
default
:
return
LocalTransactionState
.
COMMIT_MESSAGE
;
}
}
return
LocalTransactionState
.
COMMIT_MESSAGE
;
...
...
example/src/main/java/org/apache/rocketmq/example/transaction/TransactionProducer.java
浏览文件 @
61a0fcba
...
...
@@ -17,8 +17,9 @@
package
org.apache.rocketmq.example.transaction
;
import
org.apache.rocketmq.client.exception.MQClientException
;
import
org.apache.rocketmq.client.producer.LocalTransactionExecuter
;
import
org.apache.rocketmq.client.producer.SendResult
;
import
org.apache.rocketmq.client.producer.TransactionListener
;
import
org.apache.rocketmq.client.producer.Transaction
Check
Listener
;
import
org.apache.rocketmq.client.producer.TransactionMQProducer
;
import
org.apache.rocketmq.common.message.Message
;
import
org.apache.rocketmq.remoting.common.RemotingHelper
;
...
...
@@ -32,7 +33,7 @@ import java.util.concurrent.TimeUnit;
public
class
TransactionProducer
{
public
static
void
main
(
String
[]
args
)
throws
MQClientException
,
InterruptedException
{
Transaction
Listener
transactionListener
=
new
Transaction
ListenerImpl
();
Transaction
CheckListener
transactionListener
=
new
TransactionCheck
ListenerImpl
();
TransactionMQProducer
producer
=
new
TransactionMQProducer
(
"please_rename_unique_group_name"
);
ExecutorService
executorService
=
new
ThreadPoolExecutor
(
2
,
5
,
100
,
TimeUnit
.
SECONDS
,
new
ArrayBlockingQueue
<
Runnable
>(
2000
),
new
ThreadFactory
()
{
@Override
...
...
@@ -42,9 +43,9 @@ public class TransactionProducer {
return
thread
;
}
});
LocalTransactionExecuter
executor
=
new
LocalTransactionExecuterImpl
();
producer
.
setExecutorService
(
executorService
);
producer
.
setTransactionListener
(
transactionListener
);
producer
.
setTransaction
Check
Listener
(
transactionListener
);
producer
.
start
();
String
[]
tags
=
new
String
[]
{
"TagA"
,
"TagB"
,
"TagC"
,
"TagD"
,
"TagE"
};
...
...
@@ -53,7 +54,7 @@ public class TransactionProducer {
Message
msg
=
new
Message
(
"TopicTest1234"
,
tags
[
i
%
tags
.
length
],
"KEY"
+
i
,
(
"Hello RocketMQ "
+
i
).
getBytes
(
RemotingHelper
.
DEFAULT_CHARSET
));
SendResult
sendResult
=
producer
.
sendMessageInTransaction
(
msg
,
null
);
SendResult
sendResult
=
producer
.
sendMessageInTransaction
(
msg
,
executor
,
null
);
System
.
out
.
printf
(
"%s%n"
,
sendResult
);
Thread
.
sleep
(
10
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录