Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Apache RocketMQ
Rocketmq
提交
7e221e51
R
Rocketmq
项目概览
Apache RocketMQ
/
Rocketmq
上一次同步 大约 3 年
通知
267
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看板
提交
7e221e51
编写于
9月 11, 2019
作者:
Q
qqeasonchen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add example of request-response model
上级
1d3a5083
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
212 addition
and
11 deletion
+212
-11
client/src/main/java/org/apache/rocketmq/client/utils/MessageUtil.java
...in/java/org/apache/rocketmq/client/utils/MessageUtil.java
+11
-11
example/src/main/java/org/apache/rocketmq/example/rpc/AsyncRequestProducer.java
...org/apache/rocketmq/example/rpc/AsyncRequestProducer.java
+64
-0
example/src/main/java/org/apache/rocketmq/example/rpc/RequestProducer.java
...java/org/apache/rocketmq/example/rpc/RequestProducer.java
+54
-0
example/src/main/java/org/apache/rocketmq/example/rpc/ResponseConsumer.java
...ava/org/apache/rocketmq/example/rpc/ResponseConsumer.java
+83
-0
未找到文件。
client/src/main/java/org/apache/rocketmq/client/utils/MessageUtil.java
浏览文件 @
7e221e51
...
...
@@ -17,31 +17,31 @@
package
org.apache.rocketmq.client.utils
;
import
org.apache.rocketmq.client.exception.MQClientException
;
import
org.apache.rocketmq.common.MixAll
;
import
org.apache.rocketmq.common.message.Message
;
import
org.apache.rocketmq.common.message.MessageAccessor
;
import
org.apache.rocketmq.common.message.MessageConst
;
public
class
MessageUtil
{
public
static
Message
createReplyMessage
(
final
Message
requestMessage
)
{
public
static
Message
createReplyMessage
(
final
Message
requestMessage
)
throws
MQClientException
{
if
(
requestMessage
!=
null
)
{
Message
replyMessage
=
new
Message
();
String
cluster
=
requestMessage
.
getProperty
(
MessageConst
.
PROPERTY_CLUSTER
);
String
replyTo
=
requestMessage
.
getProperty
(
MessageConst
.
PROPERTY_MESSAGE_REPLY_TO
);
String
requestUniqId
=
requestMessage
.
getProperty
(
MessageConst
.
PROPERTY_REQUEST_UNIQ_ID
);
String
ttl
=
requestMessage
.
getProperty
(
MessageConst
.
PROPERTY_MESSAGE_TTL
);
if
(
cluster
==
null
)
{
if
(
cluster
!=
null
)
{
String
replyTopic
=
MixAll
.
getReplyTopic
(
cluster
);
replyMessage
.
setTopic
(
replyTopic
);
MessageAccessor
.
putProperty
(
replyMessage
,
MessageConst
.
PROPERTY_MESSAGE_TYPE
,
MixAll
.
REPLY_MESSAGE_FLAG
);
MessageAccessor
.
putProperty
(
replyMessage
,
MessageConst
.
PROPERTY_REQUEST_UNIQ_ID
,
requestUniqId
);
MessageAccessor
.
putProperty
(
replyMessage
,
MessageConst
.
PROPERTY_MESSAGE_REPLY_TO
,
replyTo
);
MessageAccessor
.
putProperty
(
replyMessage
,
MessageConst
.
PROPERTY_MESSAGE_TTL
,
ttl
);
return
replyMessage
;
}
String
replyTopic
=
MixAll
.
getReplyTopic
(
cluster
);
replyMessage
.
setTopic
(
replyTopic
);
MessageAccessor
.
putProperty
(
replyMessage
,
MessageConst
.
PROPERTY_MESSAGE_TYPE
,
MixAll
.
REPLY_MESSAGE_FLAG
);
MessageAccessor
.
putProperty
(
replyMessage
,
MessageConst
.
PROPERTY_REQUEST_UNIQ_ID
,
requestUniqId
);
MessageAccessor
.
putProperty
(
replyMessage
,
MessageConst
.
PROPERTY_MESSAGE_REPLY_TO
,
replyTo
);
MessageAccessor
.
putProperty
(
replyMessage
,
MessageConst
.
PROPERTY_MESSAGE_TTL
,
ttl
);
return
replyMessage
;
}
return
null
;
throw
new
MQClientException
(-
1
,
"create reply message fail."
)
;
}
}
example/src/main/java/org/apache/rocketmq/example/rpc/AsyncRequestProducer.java
0 → 100644
浏览文件 @
7e221e51
/*
* 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.rpc
;
import
org.apache.rocketmq.client.exception.MQClientException
;
import
org.apache.rocketmq.client.log.ClientLogger
;
import
org.apache.rocketmq.client.producer.DefaultMQProducer
;
import
org.apache.rocketmq.client.producer.RequestCallback
;
import
org.apache.rocketmq.common.message.Message
;
import
org.apache.rocketmq.logging.InternalLogger
;
import
org.apache.rocketmq.remoting.common.RemotingHelper
;
public
class
AsyncRequestProducer
{
private
static
final
InternalLogger
log
=
ClientLogger
.
getLog
();
public
static
void
main
(
String
[]
args
)
throws
MQClientException
,
InterruptedException
{
String
producerGroup
=
"ProducerGroup-Name"
;
String
namesrvAddr
=
"10.255.2.37:9876;10.255.2.37:9875"
;
String
topic
=
"RequestTopic"
;
long
ttl
=
3000
;
DefaultMQProducer
producer
=
new
DefaultMQProducer
(
producerGroup
);
producer
.
setNamesrvAddr
(
namesrvAddr
);
producer
.
start
();
try
{
Message
msg
=
new
Message
(
topic
,
""
,
"Hello world"
.
getBytes
(
RemotingHelper
.
DEFAULT_CHARSET
));
long
begin
=
System
.
currentTimeMillis
();
producer
.
request
(
msg
,
new
RequestCallback
()
{
@Override
public
void
onSuccess
(
Message
message
)
{
long
cost
=
System
.
currentTimeMillis
()
-
begin
;
System
.
out
.
printf
(
"request to <%s> cost: %d replyMessage: %s %n"
,
topic
,
cost
,
message
);
}
@Override
public
void
onException
(
Throwable
e
)
{
System
.
err
.
printf
(
"request to <%s> fail."
,
topic
);
}
},
ttl
);
}
catch
(
Exception
e
)
{
log
.
warn
(
""
,
e
);
}
// producer.shutdown();
}
}
example/src/main/java/org/apache/rocketmq/example/rpc/RequestProducer.java
0 → 100644
浏览文件 @
7e221e51
/*
* 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.rpc
;
import
org.apache.rocketmq.client.exception.MQClientException
;
import
org.apache.rocketmq.client.log.ClientLogger
;
import
org.apache.rocketmq.client.producer.DefaultMQProducer
;
import
org.apache.rocketmq.common.message.Message
;
import
org.apache.rocketmq.logging.InternalLogger
;
import
org.apache.rocketmq.remoting.common.RemotingHelper
;
public
class
RequestProducer
{
private
static
final
InternalLogger
log
=
ClientLogger
.
getLog
();
public
static
void
main
(
String
[]
args
)
throws
MQClientException
,
InterruptedException
{
String
producerGroup
=
"ProducerGroup-Name"
;
String
namesrvAddr
=
"10.255.2.37:9876;10.255.2.37:9875"
;
String
topic
=
"RequestTopic"
;
long
ttl
=
3000
;
DefaultMQProducer
producer
=
new
DefaultMQProducer
(
producerGroup
);
producer
.
setNamesrvAddr
(
namesrvAddr
);
producer
.
start
();
try
{
Message
msg
=
new
Message
(
topic
,
""
,
"Hello world"
.
getBytes
(
RemotingHelper
.
DEFAULT_CHARSET
));
long
begin
=
System
.
currentTimeMillis
();
Message
retMsg
=
producer
.
request
(
msg
,
ttl
);
long
cost
=
System
.
currentTimeMillis
()
-
begin
;
System
.
err
.
printf
(
"request to <%s> cost: %d replyMessage: %s %n"
,
topic
,
cost
,
retMsg
);
}
catch
(
Exception
e
)
{
log
.
warn
(
""
,
e
);
}
producer
.
shutdown
();
}
}
example/src/main/java/org/apache/rocketmq/example/rpc/ResponseConsumer.java
0 → 100644
浏览文件 @
7e221e51
/*
* 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.rpc
;
import
java.util.List
;
import
org.apache.rocketmq.client.consumer.DefaultMQPushConsumer
;
import
org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext
;
import
org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus
;
import
org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently
;
import
org.apache.rocketmq.client.exception.MQBrokerException
;
import
org.apache.rocketmq.client.exception.MQClientException
;
import
org.apache.rocketmq.client.log.ClientLogger
;
import
org.apache.rocketmq.client.producer.SendResult
;
import
org.apache.rocketmq.client.utils.MessageUtil
;
import
org.apache.rocketmq.common.consumer.ConsumeFromWhere
;
import
org.apache.rocketmq.common.message.Message
;
import
org.apache.rocketmq.common.message.MessageConst
;
import
org.apache.rocketmq.common.message.MessageExt
;
import
org.apache.rocketmq.logging.InternalLogger
;
import
org.apache.rocketmq.remoting.exception.RemotingException
;
public
class
ResponseConsumer
{
private
static
final
InternalLogger
log
=
ClientLogger
.
getLog
();
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
,
MQClientException
{
String
consumerGroup
=
"ConsumeGroup-Name"
;
String
namesrvAddr
=
"10.255.2.37:9873"
;
String
topic
=
"RequestTopic"
;
DefaultMQPushConsumer
consumer
=
new
DefaultMQPushConsumer
(
consumerGroup
);
consumer
.
setNamesrvAddr
(
namesrvAddr
);
consumer
.
setConsumeFromWhere
(
ConsumeFromWhere
.
CONSUME_FROM_LAST_OFFSET
);
consumer
.
registerMessageListener
(
new
MessageListenerConcurrently
()
{
@Override
public
ConsumeConcurrentlyStatus
consumeMessage
(
List
<
MessageExt
>
msgs
,
ConsumeConcurrentlyContext
context
)
{
System
.
out
.
printf
(
"%s Receive New Messages: %s %n"
,
Thread
.
currentThread
().
getName
(),
msgs
);
for
(
MessageExt
msg
:
msgs
)
{
try
{
log
.
info
(
"handle message: {} body={}"
,
msg
,
new
String
(
msg
.
getBody
()));
String
replyTo
=
msg
.
getProperty
(
MessageConst
.
PROPERTY_MESSAGE_REPLY_TO
);
//You must use MessageUtil to creage reply message, otherwise reply message maybe wrong.
Message
replyMessage
=
MessageUtil
.
createReplyMessage
(
msg
);
replyMessage
.
setBody
(
"reply message contents."
.
getBytes
());
//maybe you should create a producer to send reply message.
SendResult
sendResult
=
consumer
.
getDefaultMQPushConsumerImpl
().
getmQClientFactory
().
getDefaultMQProducer
().
send
(
replyMessage
,
3000
);
System
.
out
.
printf
(
"reply msg %s to %s , %s %n"
,
replyMessage
.
toString
(),
replyTo
,
sendResult
.
toString
());
}
catch
(
MQClientException
e
)
{
e
.
printStackTrace
();
}
catch
(
RemotingException
e
)
{
e
.
printStackTrace
();
}
catch
(
MQBrokerException
e
)
{
e
.
printStackTrace
();
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
return
ConsumeConcurrentlyStatus
.
CONSUME_SUCCESS
;
}
});
consumer
.
subscribe
(
topic
,
"*"
);
consumer
.
start
();
System
.
out
.
printf
(
"Consumer Started.%n"
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录