本文整理汇总了Java中com.alibaba.rocketmq.client.producer.SendCallback类的典型用法代码示例。如果您正苦于以下问题:Java SendCallback类的具体用法?Java SendCallback怎么用?Java SendCallback使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SendCallback类属于com.alibaba.rocketmq.client.producer包,在下文中一共展示了SendCallback类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testAsyncProducer
import com.alibaba.rocketmq.client.producer.SendCallback; //导入依赖的package包/类
@Test
public void testAsyncProducer() throws Exception {
// Instantiate with a producer group name.
DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup");
// Launch the instance.
producer.start();
producer.setRetryTimesWhenSendAsyncFailed(0);
for (int i = 0; i < 100; i++) {
final int index = i;
// Create a message instance, specifying topic, tag and message body.
Message msg = new Message("TopicTest", "TagA", "OrderID188", "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
producer.send(msg, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
System.out.printf("%-10d OK %s %n", index, sendResult.getMsgId());
}
@Override
public void onException(Throwable e) {
System.out.printf("%-10d Exception %s %n", index, e);
e.printStackTrace();
}
});
}
}
示例2: send
import com.alibaba.rocketmq.client.producer.SendCallback; //导入依赖的package包/类
/**
* KERNEL ASYNC -------------------------------------------------------
*/
public void send(Message msg, MessageQueue mq, SendCallback sendCallback) throws MQClientException,
RemotingException, InterruptedException {
// 有效性检查
this.makeSureStateOK();
Validators.checkMessage(msg, this.defaultMQProducer);
if (!msg.getTopic().equals(mq.getTopic())) {
throw new MQClientException("message's topic not equal mq's topic", null);
}
try {
this.sendKernelImpl(msg, mq, CommunicationMode.ASYNC, sendCallback);
}
catch (MQBrokerException e) {
throw new MQClientException("unknow exception", e);
}
}
示例3: sendMessage
import com.alibaba.rocketmq.client.producer.SendCallback; //导入依赖的package包/类
public SendResult sendMessage(//
final String addr,// 1
final String brokerName,// 2
final Message msg,// 3
final SendMessageRequestHeader requestHeader,// 4
final long timeoutMillis,// 5
final CommunicationMode communicationMode,// 6
final SendCallback sendCallback// 7
) throws RemotingException, MQBrokerException, InterruptedException {
RemotingCommand request = null;
if (sendSmartMsg) {
SendMessageRequestHeaderV2 requestHeaderV2 = SendMessageRequestHeaderV2.createSendMessageRequestHeaderV2(requestHeader);
request = RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE_V2, requestHeaderV2);
}
else { //组装头部信息
request = RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE, requestHeader);
}
request.setBody(msg.getBody()); //包体body信息赋值
switch (communicationMode) {
case ONEWAY:
this.remotingClient.invokeOneway(addr, request, timeoutMillis);
return null;
case ASYNC:
this.sendMessageAsync(addr, brokerName, msg, timeoutMillis, request, sendCallback);
return null;
case SYNC:
return this.sendMessageSync(addr, brokerName, msg, timeoutMillis, request);
default:
assert false;
break;
}
return null;
}
示例4: sendMessageAsync
import com.alibaba.rocketmq.client.producer.SendCallback; //导入依赖的package包/类
private void sendMessageAsync(//
final String addr,//
final String brokerName,//
final Message msg,//
final long timeoutMillis,//
final RemotingCommand request,//
final SendCallback sendCallback//
) throws RemotingException, InterruptedException {
this.remotingClient.invokeAsync(addr, request, timeoutMillis, new InvokeCallback() {
@Override
public void operationComplete(ResponseFuture responseFuture) {
if (null == sendCallback)
return;
RemotingCommand response = responseFuture.getResponseCommand();
if (response != null) {
try {
SendResult sendResult = MQClientAPIImpl.this.processSendResponse(brokerName, msg, response);
assert sendResult != null;
sendCallback.onSuccess(sendResult);
}
catch (Exception e) {
sendCallback.onException(e);
}
}
else {
if (!responseFuture.isSendRequestOK()) {
sendCallback.onException(new MQClientException("send request failed", responseFuture.getCause()));
}
else if (responseFuture.isTimeout()) {
sendCallback.onException(new MQClientException("wait response timeout " + responseFuture.getTimeoutMillis() + "ms",
responseFuture.getCause()));
}
else {
sendCallback.onException(new MQClientException("unknow reseaon", responseFuture.getCause()));
}
}
}
});
}
示例5: send
import com.alibaba.rocketmq.client.producer.SendCallback; //导入依赖的package包/类
private void send(Message msg, PacketFuture pf) throws Exception {
producer.send(msg, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
LOG.info(sendResult.toString());
pf.result(new PacketResult(PacketResult.IOSt.SEND_SUCC));
}
@Override
public void onException(Throwable e) {
LOG.error(e.getMessage(), e);
pf.result(new PacketResult(PacketResult.IOSt.SEND_FAIL));
}
});
}
示例6: sendMessageAsync
import com.alibaba.rocketmq.client.producer.SendCallback; //导入依赖的package包/类
private void sendMessageAsync(//
final String addr,//
final String brokerName,//
final Message msg,//
final long timeoutMillis,//
final RemotingCommand request,//
final SendCallback sendCallback//
) throws RemotingException, InterruptedException {
this.remotingClient.invokeAsync(addr, request, timeoutMillis, new InvokeCallback() {
@Override
public void operationComplete(ResponseFuture responseFuture) {
if (null == sendCallback)
return;
RemotingCommand response = responseFuture.getResponseCommand();
if (response != null) {
try {
SendResult sendResult = MQClientAPIImpl.this.processSendResponse(brokerName, msg, response);
assert sendResult != null;
sendCallback.onSuccess(sendResult);
} catch (Exception e) {
sendCallback.onException(e);
}
} else {
if (!responseFuture.isSendRequestOK()) {
sendCallback.onException(new MQClientException("send request failed", responseFuture.getCause()));
} else if (responseFuture.isTimeout()) {
sendCallback.onException(new MQClientException("wait response timeout " + responseFuture.getTimeoutMillis() + "ms",
responseFuture.getCause()));
} else {
sendCallback.onException(new MQClientException("unknow reseaon", responseFuture.getCause()));
}
}
}
});
}
示例7: sendSelectImpl
import com.alibaba.rocketmq.client.producer.SendCallback; //导入依赖的package包/类
private SendResult sendSelectImpl(//
Message msg,//
MessageQueueSelector selector,//
Object arg,//
final CommunicationMode communicationMode,//
final SendCallback sendCallback//
) throws MQClientException, RemotingException, MQBrokerException, InterruptedException {
// 有效性检查
this.makeSureStateOK();
Validators.checkMessage(msg, this.defaultMQProducer);
TopicPublishInfo topicPublishInfo = this.tryToFindTopicPublishInfo(msg.getTopic());
if (topicPublishInfo != null && topicPublishInfo.ok()) {
MessageQueue mq = null;
try {
mq = selector.select(topicPublishInfo.getMessageQueueList(), msg, arg);
}
catch (Throwable e) {
throw new MQClientException("select message queue throwed exception.", e);
}
if (mq != null) {
return this.sendKernelImpl(msg, mq, communicationMode, sendCallback);
}
else {
throw new MQClientException("select message queue return null.", null);
}
}
throw new MQClientException("No route info for this topic, " + msg.getTopic(), null);
}
示例8: sendMessage
import com.alibaba.rocketmq.client.producer.SendCallback; //导入依赖的package包/类
/**
* 发送消息
*/
public SendResult sendMessage(//
final String addr,// 1
final String brokerName,// 2
final Message msg,// 3
final SendMessageRequestHeader requestHeader,// 4
final long timeoutMillis,// 5
final CommunicationMode communicationMode,// 6
final SendCallback sendCallback// 7
) throws RemotingException, MQBrokerException, InterruptedException {
// 添加虚拟运行环境相关的projectGroupPrefix
if (!UtilAll.isBlank(projectGroupPrefix)) {
msg.setTopic(VirtualEnvUtil.buildWithProjectGroup(msg.getTopic(), projectGroupPrefix));
requestHeader.setProducerGroup(VirtualEnvUtil.buildWithProjectGroup(
requestHeader.getProducerGroup(), projectGroupPrefix));
requestHeader.setTopic(VirtualEnvUtil.buildWithProjectGroup(requestHeader.getTopic(),
projectGroupPrefix));
}
RemotingCommand request =
RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE, requestHeader);
request.setBody(msg.getBody());
switch (communicationMode) {
case ONEWAY:
this.remotingClient.invokeOneway(addr, request, timeoutMillis);
return null;
case ASYNC:
this.sendMessageAsync(addr, brokerName, msg, timeoutMillis, request, sendCallback);
return null;
case SYNC:
return this.sendMessageSync(addr, brokerName, msg, timeoutMillis, request);
default:
assert false;
break;
}
return null;
}
示例9: sendMessage
import com.alibaba.rocketmq.client.producer.SendCallback; //导入依赖的package包/类
/**
* 发送消息
*/
public SendResult sendMessage(//
final String addr,// 1
final String brokerName,// 2
final Message msg,// 3
final SendMessageRequestHeader requestHeader,// 4
final long timeoutMillis,// 5
final CommunicationMode communicationMode,// 6
final SendCallback sendCallback// 7
) throws RemotingException, MQBrokerException, InterruptedException {
// 添加虚拟运行环境相关的projectGroupPrefix
if (!UtilAll.isBlank(projectGroupPrefix)) {
msg.setTopic(VirtualEnvUtil.buildWithProjectGroup(msg.getTopic(), projectGroupPrefix));
requestHeader.setProducerGroup(VirtualEnvUtil.buildWithProjectGroup(
requestHeader.getProducerGroup(), projectGroupPrefix));
requestHeader.setTopic(VirtualEnvUtil.buildWithProjectGroup(requestHeader.getTopic(),
projectGroupPrefix));
}
RemotingCommand request =
RemotingCommand.createRequestCommand(MQRequestCode.SEND_MESSAGE_VALUE, requestHeader);
request.setBody(msg.getBody());
switch (communicationMode) {
case ONEWAY:
this.remotingClient.invokeOneway(addr, request, timeoutMillis);
return null;
case ASYNC:
this.sendMessageAsync(addr, brokerName, msg, timeoutMillis, request, sendCallback);
return null;
case SYNC:
return this.sendMessageSync(addr, brokerName, msg, timeoutMillis, request);
default:
assert false;
break;
}
return null;
}
示例10: sendMessage
import com.alibaba.rocketmq.client.producer.SendCallback; //导入依赖的package包/类
/**
* 发送消息到Broker
*
* @param addr broker地址
* @param brokerName
* @param msg
* @param requestHeader
* @param timeoutMillis
* @param communicationMode
* @param sendCallback
* @return
* @throws RemotingException
* @throws MQBrokerException
* @throws InterruptedException
*/
public SendResult sendMessage(//
final String addr,// 1
final String brokerName,// 2
final Message msg,// 3
final SendMessageRequestHeader requestHeader,// 4
final long timeoutMillis,// 5
final CommunicationMode communicationMode,// 6
final SendCallback sendCallback// 7
) throws RemotingException, MQBrokerException, InterruptedException {
RemotingCommand request = null;
/**
* send smart msg?
* 作用是啥?
*/
if (sendSmartMsg) {
SendMessageRequestHeaderV2 requestHeaderV2 = SendMessageRequestHeaderV2.createSendMessageRequestHeaderV2(requestHeader);
request = RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE_V2, requestHeaderV2);
} else {
request = RemotingCommand.createRequestCommand(RequestCode.SEND_MESSAGE, requestHeader);
}
request.setBody(msg.getBody());
switch (communicationMode) {
case ONEWAY:
this.remotingClient.invokeOneway(addr, request, timeoutMillis);
return null;
case ASYNC:
this.sendMessageAsync(addr, brokerName, msg, timeoutMillis, request, sendCallback);
return null;
case SYNC:
return this.sendMessageSync(addr, brokerName, msg, timeoutMillis, request);
default:
assert false;
break;
}
return null;
}
示例11: sendMessageAsync
import com.alibaba.rocketmq.client.producer.SendCallback; //导入依赖的package包/类
private void sendMessageAsync(//
final String addr,//
final String brokerName,//
final Message msg,//
final long timeoutMillis,//
final RemotingCommand request,//
final SendCallback sendCallback//
) throws RemotingException, InterruptedException {
this.remotingClient.invokeAsync(addr, request, timeoutMillis, new InvokeCallback() {
@Override
public void operationComplete(ResponseFuture responseFuture) {
if (null == sendCallback)
return;
RemotingCommand response = responseFuture.getResponseCommand();
if (response != null) {
try {
SendResult sendResult =
MQClientAPIImpl.this.processSendResponse(brokerName, msg, response);
assert sendResult != null;
sendCallback.onSuccess(sendResult);
}
catch (Exception e) {
sendCallback.onException(e);
}
}
else {
if (!responseFuture.isSendRequestOK()) {
sendCallback.onException(new MQClientException("send request failed", responseFuture
.getCause()));
}
else if (responseFuture.isTimeout()) {
sendCallback.onException(new MQClientException("wait response timeout "
+ responseFuture.getTimeoutMillis() + "ms", responseFuture.getCause()));
}
else {
sendCallback.onException(new MQClientException("unknow reseaon", responseFuture
.getCause()));
}
}
}
});
}
示例12: createDefaultSendCallback
import com.alibaba.rocketmq.client.producer.SendCallback; //导入依赖的package包/类
private SendCallback createDefaultSendCallback(){
return new DefaultSendCallback();
}