14.1 异步消息与异步调用==ActiveMQ的集成应用

依赖引入

<!--集成ActivMQ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>

配置文件配置

####ActiveMQ配置
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
spring.activemq.packages.trust-all=true

开发生产消费者,进行消息队列的初步使用

创建生产者

package com.example.demo.activemq;

import com.example.demo.model.AyMood;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import javax.jms.Destination;

/**
 * @program: demo
 * @description: 微信说说生产
 * @author: wllmp520
 * @create: 2019-06-19 17:10
 */
@Service
public class AyMoodProducer {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    public  void sendMessage(Destination destination, final String message){
        jmsMessagingTemplate.convertAndSend(destination,message);
    }
 
}

创建消费者

package com.example.demo.activemq;

import com.example.demo.model.AyMood;
import com.example.demo.service.AyMoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * @program: demo
 * @description: 微信说说消费者
 * @author: wllmp520
 * @create: 2019-06-19 17:15
 */
@Component
public class AyMoodConsumer {
    @Autowired
    private AyMoodService ayMoodService;
    @JmsListener(destination = "ayqueue")
    public void recieveQueue(String text){
        //将是对获取信息的处理
        System.out.println("用户发表说说:"+text);
    }
 
}

单元测试

  @Test
    public void testActiveMQ1(){
       Destination destination=new ActiveMQQueue("ayqueue");
       ayMoodProducer.sendMessage(destination,"初步试用ActivMQ");
    }

结果:

简单的队列消费就已经完成了,下面使用这种方式 创建 对用户信息的保存 队列服务。

创建数据库表

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for ay_mood
-- ----------------------------
DROP TABLE IF EXISTS `ay_mood`;
CREATE TABLE `ay_mood`  (
  `id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `content` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
  `user_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
  `praise_num` int(11) NULL DEFAULT NULL,
  `publish_time` datetime(0) NULL DEFAULT NULL,
  INDEX `mood_user_id_index`(`user_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

创建实体类

@Entity
@Table(name = "ay_mood")
@Data
public class AyMood implements Serializable {
    @Id
    private String id;
    private  String content;
    private  String userId;
    private Integer praiseNum;
    private Date publishTime;
}

创建Dao层 ,集成JpaRepository的所有方法 

public interface AyMoodRepository extends JpaRepository<AyMood,String> {
}

创建service实现层

/**
 * @program: demo
 * @description: 微信说说服务实现层
 * @author: wllmp520
 * @create: 2019-06-19 16:56
 */
@Service
public class AyMoodServiceImpl implements AyMoodService {
    private  static   Destination destination=new ActiveMQQueue("ayqueue.asynsave");
    @Autowired
    private AyMoodProducer ayMoodProducer;

    @Autowired
    private AyMoodRepository ayMoodRepository;

    @Override
    public AyMood save(AyMood ayMood) {
        return ayMoodRepository.save(ayMood);
    }

    @Override
    public String aysncSave(AyMood ayMood) {
        ayMoodProducer.sendMessage(destination,ayMood);
        return "success";
    }
}

下一章进行实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值