public class rockermq1 { public static void main(String[] args) { DefaultMQProducer defaultMQProducer = new DefaultMQProducer("GROUP",getAclRPCHook()); // 1 指定NameServer的地址 //defaultMQProducer.setNamesrvAddr("10.10.1.19:9876"); // 2 设置消息同步发送失败时的重试次数,默认为 2 defaultMQProducer.setRetryTimesWhenSendFailed(2); // 3 设置消息发送超时时间,默认3000ms defaultMQProducer.setSendMsgTimeout(3000); // 4 这里设置为false defaultMQProducer.setVipChannelEnabled(false); try { // 5 启动生产者 defaultMQProducer.start(); Message message = new Message("topic-test", "test-tag", "测试数据001".getBytes()); // 2 异步发送消息 SendResult s = defaultMQProducer.send(message); System.out.println("发送的消息:"+s); } catch (Exception e) { //log.error("启动rocketMq生产者异常", e); System.out.println("错误:"+e); } } //设置账号密码 static RPCHook getAclRPCHook() { return new AclClientRPCHook(new SessionCredentials("xxx", "xxx")); } }
maven
</dependency> <dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-client</artifactId> <version>4.8.0</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-acl</artifactId> <version>4.8.0</version> </dependency> </dependencies>