1. 程式人生 > >springboot整合activemq簡單例子,附上原始碼連結

springboot整合activemq簡單例子,附上原始碼連結

  1. 在springboot的官網拿到工程,並且匯入activeMq的依賴

  1. application.properties檔案

 spring.activemq.broker-url=tcp://192.168.66.43:61616 

spring.activemq.in-memory=true

spring.activemq.pool.enabled=false  

(如果 spring.activemq.pool.enabled=true,那麼需要加入activemq連線池的依賴:

<dependency>

<groupId>org.apache.activemq</groupId>

<artifactId>activemq-pool</artifactId>

<!-- <version>5.7.0</version> -->

</dependency>

  1. producer的寫法

   使用JmsMessagingTemplate的convertAndSend(destination,message)方法,將資訊傳送到activeMq的伺服器

  1. consumer寫法

    使用@JmsListener(destination=”在producer中傳過來的dstination值”)

 

  1. 測試(使用springboot的測試)

  1. 犯過的一個錯誤

測試的時候,未在testActivemq方法上面加一個@Test註解,導致出現錯誤:

在springboot使用test測試。如果在方法上沒有加@Test註解,就會出現這樣的錯誤:java.lang.Exception: No runnable methods

at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)

at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)

at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)

at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)

at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)

at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:137)

at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49)

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.lang.reflect.Constructor.newInstance(Constructor.java:423)

at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)

at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)

at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)

at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)

at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)

at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:87)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:73)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:46)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:522)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)