`
tangxininjava
  • 浏览: 94013 次
  • 性别: Icon_minigender_1
  • 来自: 遂宁
社区版块
存档分类
最新评论

spring boot rabbitmq

阅读更多

http://tangxininjava.iteye.com/blog/2276211

在上面这篇基础上 使用rabbitmq 当然首先需要安装rabbitmq。关于rabbitmq的安装就不说了。

我安装的windows版本的 安装好了之后访问地址:http://localhost:15672/#/

默认登录用户名:guest 密码:guest

 

 

因为使用的spring boot 代码如下:

Application.java

package com.zmall;

import com.rabbitmq.client.Channel;
import com.zmall.service.Receiver;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.MultipartConfigFactory;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;

import javax.servlet.MultipartConfigElement;
import javax.sql.DataSource;

@Configuration
@EnableAutoConfiguration
@MapperScan(basePackages = "com.zmall.mapper")
@ComponentScan(basePackages = "com.zmall")
public class Application extends SpringBootServletInitializer {

    final static String queueName = "o2o.zmall.goods";



    @Bean
    Queue queue() {
        return new Queue(queueName, false);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("o2o.zmall");
    }

    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(queueName);
    }

    @Bean
    SimpleMessageListenerContainer container(MessageListenerAdapter listenerAdapter) {
        com.rabbitmq.client.ConnectionFactory client = new com.rabbitmq.client.ConnectionFactory();
        client.setHost("localhost");
        client.setPort(5672);
        ConnectionFactory connectionFactory = new CachingConnectionFactory(client);
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(queueName);
        container.setMessageListener(listenerAdapter);
        return container;
    }

    @Bean
    Receiver receiver() {
        return new Receiver();
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("5MB");
        factory.setMaxRequestSize("5MB");
        return factory.createMultipartConfig();
    }


    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }


    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        return sessionFactory.getObject();
    }
}

 然后控制器中发送信息 IndexController.java

package com.zmall.controller;

import com.zmall.model.City;
import com.zmall.service.CityService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Administrator on 2016/2/10.
 */
@Controller
@RequestMapping(value = "/index")
public class IndexController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    private CityService cityService;

    @RequestMapping(value = "/get",method = RequestMethod.GET)
    @ResponseBody
    public String index(){
        City city = cityService.getCity(1L);
        rabbitTemplate.convertAndSend("o2o.zmall.goods","Hello from RabbitMQ!");
        return city.getName();
    }


}

 

在上面的Application.java中我们可以看到这个代码:

@Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }

 这个Receiver就是我们自己手动创建的 代码如下 Receiver.java

package com.zmall.service;

/**
 * Created by Administrator on 2016/2/12.
 */
public class Receiver {
    public void receiveMessage(String message) {
        System.out.println("Received <" + message + ">");
    }
}

 

 启动tomcat 访问地址:http://localhost:8080/index/get

 看到控制台打印信息:Received <Hello from RabbitMQ!>

 

 

 总结:RabbitMQ和我们数据库连接 hibernate spring 都有工厂 都有类似连接这样的概念。

 Connection 这种概念就是我们配置上面都有类似性,可以通过查询源码来看到这些信息。

比如看下这个com.rabbitmq.client.ConnectionFactory的源码

/** @deprecated */
    @Deprecated
    public static final int DEFAULT_NUM_CONSUMER_THREADS = 5;
    public static final String DEFAULT_USER = "guest";
    public static final String DEFAULT_PASS = "guest";
    public static final String DEFAULT_VHOST = "/";
    public static final int DEFAULT_CHANNEL_MAX = 0;
    public static final int DEFAULT_FRAME_MAX = 0;
    public static final int DEFAULT_HEARTBEAT = 0;
    public static final String DEFAULT_HOST = "localhost";
    public static final int USE_DEFAULT_PORT = -1;
    public static final int DEFAULT_AMQP_PORT = 5672;
    public static final int DEFAULT_AMQP_OVER_SSL_PORT = 5671;
    public static final int DEFAULT_CONNECTION_TIMEOUT = 0;
    public static final int DEFAULT_SHUTDOWN_TIMEOUT = 10000;
    private static final String DEFAULT_SSL_PROTOCOL = "TLSv1";
    private String username = "guest";
    private String password = "guest";
    private String virtualHost = "/";
    private String host = "localhost";
    private int port = -1;
    private int requestedChannelMax = 0;
    private int requestedFrameMax = 0;
    private int requestedHeartbeat = 0;
    private int connectionTimeout = 0;
    private int shutdownTimeout = 10000;
    private Map<String, Object> _clientProperties = AMQConnection.defaultClientProperties();
    private SocketFactory factory = SocketFactory.getDefault();
    private SaslConfig saslConfig;
    private ExecutorService sharedExecutor;
    private ThreadFactory threadFactory;
    private SocketConfigurator socketConf;
    private ExceptionHandler exceptionHandler;
    private boolean automaticRecovery;
    private boolean topologyRecovery;
    private long networkRecoveryInterval;

 从上面的源码我们可以看出 凡是都是可配置的。这些代码的配置替代xml也是可以的。xml有时候明朗化都可以。不同的方式配置不一样而已。

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics