<!-- jms -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
# ACTIVEMQ (ActiveMQProperties)
spring.activemq.in-memory=true
#spring.activemq.broker-url=
#spring.activemq.password=
#spring.activemq.user=
#spring.activemq.packages.trust-all=false
#spring.activemq.packages.trusted=
#spring.activemq.pool.configuration.*=
#spring.activemq.pool.enabled=false
#spring.activemq.pool.expiry-timeout=0
#spring.activemq.pool.idle-timeout=30000
#spring.activemq.pool.max-connections=1
package com.shuju.test2.util.configuration;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.jms.Queue;
@Configuration
public class JmsConfiguration {
@Bean
public Queue queue() {
return new ActiveMQQueue("demo.queue");
}
}
package com.shuju.test2.component;
import javax.jms.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
@Component
public class DemoJmsComponent {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired
private Queue queue;
public void send(String msg) {
this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
}
@JmsListener(destination = "demo.queue")
public void receiveQueue(String text) {
System.out.println("接受到:" + text);
}
}
package com.shuju.test2;
import com.shuju.test2.component.DemoJmsComponent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test2ApplicationTests {
@Autowired
private DemoJmsComponent demoJmsComponent;
@Test
public void send() {
demoJmsComponent.send("hello world");
}
}