济南达内培训的老师说,创建应用主类:
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}
创建单元测试类,用来调用消息生产:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = HelloApplication.class)
public class HelloApplicationTests {
@Autowired
private Sender sender;
@Test
public void hello() throws Exception {
sender.send();
}
}
完成程序编写之后,下面开始尝试运行。首先确保RabbitMQ Server已经开始,然后进行下面的操作:
启动应用主类,从控制台中,我们看到如下内容,程序创建了一个访问127.0.0.1:5672中springcloud的连接。

o.s.a.r.c.CachingConnectionFactory : Created new connection: SimpleConnection@29836d32 [delegate=amqp://springcloud@127.0.0.1:5672/]
同时,我们通过RabbitMQ的控制面板,可以看到Connection和Channels中包含当前连接的条目。
运行单元测试类,我们可以看到控制台中输出下面的内容,消息被发送到了RabbitMQ Server的hello队列中。
Sender : hello Sun Sep 25 11:06:11 CST 2016
切换到应用主类的控制台,我们可以看到类似如下输出,消费者对hello队列的监听程序执行了,并输出了接受到的消息信息。
Receiver : hello Sun Sep 25 11:06:11 CST 2016
通过上面的示例,我们在Spring Boot应用中引入spring-boot-starter-amqp模块,进行简单配置就完成了对RabbitMQ的消息生产和消费的开发内容。然而在实际应用中,我们还有很多内容没有演示,这里不做更多的讲解,读者可以自行查阅RabbitMQ的官方教程,有更全面的了解。
完整示例:Chapter5-2-1
开源中国:#/didispace/SpringBoot-Learning/tree/master/Chapter5-2-1
GitHub:https://github.com/dyc87112/SpringBoot-Learning/tree/master/Chapter5-2-1
以上就是济南达内培训给大家做的内容详解,更多关于IT的学习,请继续关注济南达内培训