赞
踩
- org.apache.rocketmq.client.exception.MQClientException: The producer service state not OK, maybe started once, RUNNING
- See http://rocketmq.apache.org/docs/faq/ for further details.
-
- at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.start(DefaultMQProducerImpl.java:185)
- at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.start(DefaultMQProducerImpl.java:146)
- at org.apache.rocketmq.client.producer.DefaultMQProducer.start(DefaultMQProducer.java:172)
- at com.zengjx.mq.SpringMQTest.testSendMsg(SpringMQTest.java:37)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
- at java.lang.reflect.Method.invoke(Method.java:498)
- at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
- at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
- at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
- at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
参考:
https://blog.csdn.net/magicianofcodes/article/details/100540135
问题原因:
用注解的形式不用自己去启动 producer 服务,所以吧
producer.start() 的代码删除就好
- package com.zengjx.mq;
-
- import org.apache.rocketmq.client.producer.DefaultMQProducer;
- import org.apache.rocketmq.client.producer.SendResult;
- import org.apache.rocketmq.common.message.Message;
- import org.apache.rocketmq.remoting.common.RemotingHelper;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- import org.springframework.test.context.junit4.SpringRunner;
-
- import java.io.IOException;
-
- /**
- * @ClassName HelloController
- * @Description TODO
- * @Author zengjx
- * @Company zengjx
- * @Date 2019/12/9 20:24
- * @Version V1.0
- */
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = "classpath*:applicationContext-producer.xml")
- public class SpringMQTest {
- @Autowired
- private DefaultMQProducer defaultMQProducer ;
-
- @Test
- public void testSendMsg() throws Exception{
- //1.创建生产者
- // DefaultMQProducer defaultMQProducer =new DefaultMQProducer("productGroup");
- //2.设置NameServerd 地址
- defaultMQProducer.setNamesrvAddr("127.0.0.1:9876");//出错位置
- // 3.启动生产者producer.start()
- defaultMQProducer.start();//注释掉 出错位置
- //4.创建消息message
- //Message(String topic, String tags, String keys, int flag, byte[] body, boolean waitStoreMsgOK)
- Message message=new Message("topic-spring",
- "tag-spring",
- "key-spring",
- "这是我第一次发送RocketMQ".getBytes(RemotingHelper.DEFAULT_CHARSET));
- SendResult sendResult = defaultMQProducer.send(message);
- System.out.println("sendRsult:"+sendResult);
- // defaultMQProducer.shutdown();
- //5.发送消息 接收结果sendResult
- //6.输出sendResult 查看是否成功
- //7.如果不再发送消息关闭生产者
-
-
-
- }
配置 :
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
- <!--创建 一个 DefaultMQProducter
- init 对象创建后
- destroy 对象销毁之后
-
- -->
- <bean id="defaultMQProducter" class="org.apache.rocketmq.client.producer.DefaultMQProducer"
- init-method="start" destroy-method="shutdown"
- >
- <!-- 设置组名 -->
- <property name="producerGroup" value="spring-producerGroup">
-
- </property>
- <!--设置NameServer地址-->
- <property name="namesrvAddr" value="127.0.0.1:9876">
-
- </property>
-
- </bean>
- </beans>
- package com.zengjx.mq;
-
- import org.apache.rocketmq.client.producer.DefaultMQProducer;
- import org.apache.rocketmq.client.producer.SendResult;
- import org.apache.rocketmq.common.message.Message;
- import org.apache.rocketmq.remoting.common.RemotingHelper;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-
- /**
- * @ClassName HelloController
- * @Description TODO
- * @Author zengjx
- * @Company zengjx
- * @Date 2019/12/9 21:49
- * @Version V1.0
- */
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = "classpath*:applicationContext-producer.xml")
- public class SpringMQTest2 {
- @Autowired
- private DefaultMQProducer producer;
-
- @Test
- public void testSendMsg() throws Exception{
- //4.创建消息-message = new Message(主题名,标签名,消息key名,消息内容.getBytes(RemotingHelper.DEFAULT_CHARSET));
- Message message = new Message(
- "topic-spring",
- "tag-1",
- "key-1",
- "这是我第1次发送Spring-MQ消息".getBytes(RemotingHelper.DEFAULT_CHARSET)
- );
- //5.发送消息,接收结果-sendResult = producer.send(message)
- SendResult sendResult = producer.send(message);
- //6.输出sendResult查看消息是否成功送达
- System.out.println(sendResult);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。