RabbitMq应用在发送短信

RabbitMq应用在发送短信(本文应用生产-消费模式):

首先安装mq在机器上,注册用户名和密码

短信发送分为server端和client端:

在maven中配置下载mq的依赖包

<dependency>
    <groupId>org.springframework.amqpgroupId>
    <artifactId>spring-rabbitartifactId>
    <version>1.4.3.RELEASEversion>
dependency>

rabbitMQ.properties的配置

rabbit_server_username=用户名
rabbit_server_password=密码
rabbit_server_host=ip地址
rabbit_heart_beat=120

client端的调用:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:rabbitMQ.properties" />
bean>


<rabbit:connection-factory id="connectionFactory" host="${rabbit_server_host}" username="${rabbit_server_username}" password="${rabbit_server_password}"  requested-heartbeat="${rabbit_heart_beat}" />

<rabbit:admin connection-factory="connectionFactory"/>


<rabbit:queue id="queue名字" durable="true" auto-delete="false" exclusive="false" name="queue名字"/>


<rabbit:direct-exchange name="exchange名字" >
    <rabbit:bindings>  
        <rabbit:binding key="queue名字" queue="queue名字"/>  
    rabbit:bindings>  
rabbit:direct-exchange>
<rabbit:template exchange="exchange名字" id="amqpTemplate" queue="queue名字" connectionfactory="connectionFactory"  message-converter="传递方式MessageConvertConfig"/>

然后在自己的方法中调用

public void send(){
    @Autowired
    private AmqpTemplate amqpTemplate;

    public void sendSMS(){
        amqpTemplate.convertAndSend(obj);
    }
}

server端的配置和编写

在spring中配置mq


<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:rabbitMQ.properties" />
bean>


<rabbit:connection-factory id="connectionFactory" host="${rabbit_server_host}" username="${rabbit_server_username}" password="${rabbit_server_password}"  requested-heartbeat="${rabbit_heart_beat}" />

<rabbit:admin connection-factory="connectionFactory"/>


<rabbit:queue id="queue名字" durable="true" auto-delete="false" exclusive="false" name="queue名字"/>


<rabbit:direct-exchange name="exchange名字" >
    <rabbit:bindings>  
        <rabbit:binding key="queue名字" queue="queue名字"/>  
    rabbit:bindings>  
rabbit:direct-exchange>


<bean id="实现类id" class="实现类" />  

<rabbit:listener-container connection-factory="connectionFactory">
    <rabbit:listener ref="实现类id" queue-names="queue名字"/>
rabbit:listener-container> 

实现类编写

public class 实现类 implements MessageListener {
    @Override
    public void onMessage(Message message) {

        //1、MessageConvertConfig如果有加密的方法要先解密(和发送端保持一致)

        //2、解析参数

        //3、自己的逻辑处理。。。

        //4、调用发送短信的接口(第三方)
    }
}

实现MessageListener 的接口监听消息队列,当有消息发送到队列中时就会调用到服务端的onMessage方法

实现发送到队列消息

rabbitmq安装教程:http://blog.csdn.net/historyasamirror/article/details/6827870

你可能感兴趣的:(java,java,rabbitmq,短信发送,短信,应用)