|
| 1 | +/* |
| 2 | +* Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +* contributor license agreements. See the NOTICE file distributed with |
| 4 | +* this work for additional information regarding copyright ownership. |
| 5 | +* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +* (the "License"); you may not use this file except in compliance with |
| 7 | +* the License. You may obtain a copy of the License at |
| 8 | +* |
| 9 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +* |
| 11 | +* Unless required by applicable law or agreed to in writing, software |
| 12 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +* See the License for the specific language governing permissions and |
| 15 | +* limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +#include <stdio.h> |
| 19 | + |
| 20 | +#include "CProducer.h" |
| 21 | +#include "CCommon.h" |
| 22 | +#include "CMessage.h" |
| 23 | +#include "CSendResult.h" |
| 24 | + |
| 25 | +#ifdef _WIN32 |
| 26 | +#include <windows.h> |
| 27 | +#else |
| 28 | + |
| 29 | +#include <unistd.h> |
| 30 | +#include <memory.h> |
| 31 | + |
| 32 | +#endif |
| 33 | + |
| 34 | +void thread_sleep(unsigned milliseconds) { |
| 35 | +#ifdef _WIN32 |
| 36 | + Sleep(milliseconds); |
| 37 | +#else |
| 38 | + usleep(milliseconds * 1000); // takes microseconds |
| 39 | +#endif |
| 40 | +} |
| 41 | + |
| 42 | +void sendSuccessCallback(CSendResult result){ |
| 43 | + printf("Msg Send ID:%s\n", result.msgId); |
| 44 | +} |
| 45 | + |
| 46 | +void sendExceptionCallback(CMQException e){ |
| 47 | + printf("asyn send exception error : %d\n" , e.error); |
| 48 | + printf("asyn send exception msg : %s\n" , e.msg); |
| 49 | + printf("asyn send exception file : %s\n" , e.file); |
| 50 | + printf("asyn send exception line : %d\n" , e.line); |
| 51 | +} |
| 52 | + |
| 53 | +void startSendMessage(CProducer *producer) { |
| 54 | + int i = 0; |
| 55 | + char DestMsg[256]; |
| 56 | + CMessage *msg = CreateMessage("T_TestTopic"); |
| 57 | + SetMessageTags(msg, "Test_Tag"); |
| 58 | + SetMessageKeys(msg, "Test_Keys"); |
| 59 | + CSendResult result; |
| 60 | + for (i = 0; i < 10; i++) { |
| 61 | + printf("send one message : %d\n", i); |
| 62 | + memset(DestMsg, 0, sizeof(DestMsg)); |
| 63 | + snprintf(DestMsg, sizeof(DestMsg), "New message body: index %d", i); |
| 64 | + SetMessageBody(msg, DestMsg); |
| 65 | + int code = SendMessageAsync(producer, msg, sendSuccessCallback , sendExceptionCallback); |
| 66 | + printf("Async send return code: %d\n", code); |
| 67 | + thread_sleep(1000); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +void CreateProducerAndStartSendMessage(int i){ |
| 72 | + printf("Producer Initializing.....\n"); |
| 73 | + CProducer *producer = CreateProducer("Group_producer"); |
| 74 | + SetProducerNameServerAddress(producer, "127.0.0.1:9876"); |
| 75 | + if(i == 1){ |
| 76 | + SetProducerSendMsgTimeout(producer , 3); |
| 77 | + } |
| 78 | + StartProducer(producer); |
| 79 | + printf("Producer start.....\n"); |
| 80 | + startSendMessage(producer); |
| 81 | + ShutdownProducer(producer); |
| 82 | + DestroyProducer(producer); |
| 83 | + printf("Producer Shutdown!\n"); |
| 84 | +} |
| 85 | + |
| 86 | +int main(int argc, char *argv[]) { |
| 87 | + printf("Send Async successCallback.....\n"); |
| 88 | + CreateProducerAndStartSendMessage(0); |
| 89 | + |
| 90 | + printf("Send Async exceptionCallback.....\n"); |
| 91 | + CreateProducerAndStartSendMessage(1); |
| 92 | + |
| 93 | + return 0; |
| 94 | +} |
| 95 | + |
0 commit comments