当前位置:   article > 正文

RocketMq:MQClientException The producer service state not OK,RUNNING_expected the service producerimpl-0 [failed] to be

expected the service producerimpl-0 [failed] to be running, but the service
  1. org.apache.rocketmq.client.exception.MQClientException: The producer service state not OK, maybe started once, RUNNING
  2. See http://rocketmq.apache.org/docs/faq/ for further details.
  3. at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.start(DefaultMQProducerImpl.java:185)
  4. at org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.start(DefaultMQProducerImpl.java:146)
  5. at org.apache.rocketmq.client.producer.DefaultMQProducer.start(DefaultMQProducer.java:172)
  6. at com.zengjx.mq.SpringMQTest.testSendMsg(SpringMQTest.java:37)
  7. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  8. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  9. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  10. at java.lang.reflect.Method.invoke(Method.java:498)
  11. at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  12. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  13. at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  14. at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

 参考:

https://blog.csdn.net/magicianofcodes/article/details/100540135

 问题原因:

用注解的形式不用自己去启动 producer 服务,所以吧
producer.start() 的代码删除就好

  1. package com.zengjx.mq;
  2. import org.apache.rocketmq.client.producer.DefaultMQProducer;
  3. import org.apache.rocketmq.client.producer.SendResult;
  4. import org.apache.rocketmq.common.message.Message;
  5. import org.apache.rocketmq.remoting.common.RemotingHelper;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.test.context.ContextConfiguration;
  10. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  11. import org.springframework.test.context.junit4.SpringRunner;
  12. import java.io.IOException;
  13. /**
  14. * @ClassName HelloController
  15. * @Description TODO
  16. * @Author zengjx
  17. * @Company zengjx
  18. * @Date 2019/12/9 20:24
  19. * @Version V1.0
  20. */
  21. @RunWith(SpringJUnit4ClassRunner.class)
  22. @ContextConfiguration(locations = "classpath*:applicationContext-producer.xml")
  23. public class SpringMQTest {
  24. @Autowired
  25. private DefaultMQProducer defaultMQProducer ;
  26. @Test
  27. public void testSendMsg() throws Exception{
  28. //1.创建生产者
  29. // DefaultMQProducer defaultMQProducer =new DefaultMQProducer("productGroup");
  30. //2.设置NameServerd 地址
  31. defaultMQProducer.setNamesrvAddr("127.0.0.1:9876");//出错位置
  32. // 3.启动生产者producer.start()
  33. defaultMQProducer.start();//注释掉 出错位置
  34. //4.创建消息message
  35. //Message(String topic, String tags, String keys, int flag, byte[] body, boolean waitStoreMsgOK)
  36. Message message=new Message("topic-spring",
  37. "tag-spring",
  38. "key-spring",
  39. "这是我第一次发送RocketMQ".getBytes(RemotingHelper.DEFAULT_CHARSET));
  40. SendResult sendResult = defaultMQProducer.send(message);
  41. System.out.println("sendRsult:"+sendResult);
  42. // defaultMQProducer.shutdown();
  43. //5.发送消息 接收结果sendResult
  44. //6.输出sendResult 查看是否成功
  45. //7.如果不再发送消息关闭生产者
  46. }

配置 :

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <!--创建 一个 DefaultMQProducter
  6. init 对象创建后
  7. destroy 对象销毁之后
  8. -->
  9. <bean id="defaultMQProducter" class="org.apache.rocketmq.client.producer.DefaultMQProducer"
  10. init-method="start" destroy-method="shutdown"
  11. >
  12. <!-- 设置组名 -->
  13. <property name="producerGroup" value="spring-producerGroup">
  14. </property>
  15. <!--设置NameServer地址-->
  16. <property name="namesrvAddr" value="127.0.0.1:9876">
  17. </property>
  18. </bean>
  19. </beans>

 

  1. package com.zengjx.mq;
  2. import org.apache.rocketmq.client.producer.DefaultMQProducer;
  3. import org.apache.rocketmq.client.producer.SendResult;
  4. import org.apache.rocketmq.common.message.Message;
  5. import org.apache.rocketmq.remoting.common.RemotingHelper;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.test.context.ContextConfiguration;
  10. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  11. /**
  12. * @ClassName HelloController
  13. * @Description TODO
  14. * @Author zengjx
  15. * @Company zengjx
  16. * @Date 2019/12/9 21:49
  17. * @Version V1.0
  18. */
  19. @RunWith(SpringJUnit4ClassRunner.class)
  20. @ContextConfiguration(locations = "classpath*:applicationContext-producer.xml")
  21. public class SpringMQTest2 {
  22. @Autowired
  23. private DefaultMQProducer producer;
  24. @Test
  25. public void testSendMsg() throws Exception{
  26. //4.创建消息-message = new Message(主题名,标签名,消息key名,消息内容.getBytes(RemotingHelper.DEFAULT_CHARSET));
  27. Message message = new Message(
  28. "topic-spring",
  29. "tag-1",
  30. "key-1",
  31. "这是我第1次发送Spring-MQ消息".getBytes(RemotingHelper.DEFAULT_CHARSET)
  32. );
  33. //5.发送消息,接收结果-sendResult = producer.send(message)
  34. SendResult sendResult = producer.send(message);
  35. //6.输出sendResult查看消息是否成功送达
  36. System.out.println(sendResult);
  37. }
  38. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/257010
推荐阅读
相关标签
  

闽ICP备14008679号