搭建activemq

1.pom.xml里面:

<!-- activemq  jmsTemplate-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
            <version>5.14.5</version>
        </dependency>

2.application.properties里面:

#activemq
spring.activemq.broker-url: tcp://localhost:61616
spring.activemq.user: admin
spring.activemq.password: admin
#true是topic,说明:生产者,消费者抢,同时,两条,不会保存
#flase是queue,说明:11 有记录 安全
#spring.jms.pub-sub-domain: true
spring.activemq.pool.enabled: false
spring.activemq.pool.max-connections=50
#spring.activemq.pool.expiry-timeout=10000
spring.activemq.pool.idle-timeout=30000
spring.application.name: influxdb-demo

3.ProducerController里面:

package com.tenoic.qq.controller;

import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.jms.Destination;


/**
 * @auther xu
 * @date 2019-10-23 13:48
 * @function  生产者
 * @return
 */

@Component
public class ProducerController {

    @Resource
    private JmsMessagingTemplate jmsMessagingTemplate;

    public void sendMessage(Destination destination, String message){

        jmsMessagingTemplate.convertAndSend(destination, message);
    }

}

4.Listener:我起的名字QqApplication:

package com.tenoic.qq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.event.EventListener;

import java.awt.*;
import java.io.IOException;
import java.net.URISyntaxException;

@SpringBootApplication
@EnableCaching
public class QqApplication {
    /*主启动*/
    public static void main(String[] args) {
        SpringApplication.run(QqApplication.class, args);
        System.out.println("端口启动");

    }
    /*当端口启动后,直接跳转界面*/
    @EventListener({ApplicationReadyEvent.class})
    void applicationReadyEvent() {
        System.out.println("应用已经准备就绪 ... 启动浏览器");
        String url = "http://localhost:8089/system/index.jsp";
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

5.QqApplicationTests里面:

package com.tenoic.qq;

import com.tenoic.qq.controller.ProducerController;
import org.apache.activemq.command.ActiveMQQueue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import javax.jms.Destination;

@RunWith(SpringRunner.class)
@SpringBootTest
public class QqApplicationTests {

    @Resource
    private ProducerController producerController;

    @Test
    public void contextLoads() {
        Destination destination = new ActiveMQQueue("message.queue");
        producerController.sendMessage(destination,"[//传入json]");
    }
}

6.CoumterController里面:

package com.tenoic.qq.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tenoic.qq.configs.InfluxDBConfig;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;


/**
 * @auther xu
 * @date 2019-10-23 13:39
 * @function  消费者
 * @return
 */
@RestController
@Component
public class CoumterController {

    private InfluxDBConfig influxDBConfig;

    @JmsListener(destination = "message.queue")

    public void  receiveQueue(String jsonobject){

        influxDBConfig = InfluxDBConfig.setUp();
        /*接收消息*/
        System.out.println("收到消息 =["+jsonobject+"]");
        /*解析json,知道key*/
        JSONArray jsonArray1 = JSON.parseArray(jsonobject);
        JSONArray jsonArray = new JSONArray(jsonArray1);
        /*Map contents = new HashMap<>();*/
        for(int i=0;i<jsonArray.size();i++) {
            JSONObject object = jsonArray.getJSONObject(i);
            String id = object.getString("id");
            String name = object.getString("name");
            String bb = object.getString("bb");
            String timeData = object.getString("timeData");
            /*获取当前时间转为yyyy-mm-dd*/
            long currentTimeMillis = System.currentTimeMillis();
           /* String timeStr= LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));*/
            /*UTC时间加上8小时*/
            /*插入时序数据库*/
            Map<String,String> tags = new HashMap<>();
            tags.put("id",id);
            Map<String,Object> fields = new HashMap<>();
            fields.put("name",name);
            fields.put("bb",bb);
            fields.put("timeData",timeData);
            fields.put("currentTimeMillis",currentTimeMillis);
            influxDBConfig.insert("",tags,fields);

        }
    }
}

7.activemq下载,登录http://localhost:8161 查看发送消息,接收消息等

你可能感兴趣的:(java)