2021-08-05

This commit is contained in:
2021-08-05 13:33:52 +09:00
parent d45139dd58
commit 0f4d342354
32 changed files with 743 additions and 4 deletions

27
mqtt/build.gradle.kts Normal file
View File

@@ -0,0 +1,27 @@
/*
* Spring-boot Examples
*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
plugins {
id("elex-spring-boot")
id("org.springframework.boot") version "2.5.3"
id("io.spring.dependency-management") version "1.0.11.RELEASE"
}
dependencies {
implementation ("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-mustache")
implementation ("org.springframework.boot:spring-boot-starter-integration")
implementation ("org.springframework.integration:spring-integration-mqtt")
compileOnly ("org.projectlombok:lombok")
developmentOnly ("org.springframework.boot:spring-boot-devtools")
annotationProcessor ("org.projectlombok:lombok")
testImplementation ("org.springframework.boot:spring-boot-starter-test")
}

View File

@@ -0,0 +1,18 @@
/*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import com.samskivert.mustache.Mustache;
import org.springframework.boot.autoconfigure.mustache.MustacheEnvironmentCollector;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
@Configuration
public class Config {
@Bean
public Mustache.Compiler mustacheCompiler(
Mustache.TemplateLoader templateLoader,
Environment environment) {
MustacheEnvironmentCollector collector
= new MustacheEnvironmentCollector();
collector.setEnvironment(environment);
return Mustache.compiler()
.withLoader(templateLoader)
.withCollector(collector);
}
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@RestController
public class HttpController {
@Autowired
private MqttPublisher mqtt;
@GetMapping("/")
public ModelAndView home() {
final Map<String, Object> map = new HashMap<>();
return new ModelAndView("home", map);
}
@GetMapping("/publish")
public void publish(@RequestParam String topic, @RequestParam String message) {
log.info("I'll publish a message: {} {}", topic, message);
mqtt.publish(topic, message);
}
}

View File

@@ -0,0 +1,92 @@
/*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import org.eclipse.paho.client.mqttv3.MqttAsyncClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.core.MessageProducer;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
/**
* @see "https://docs.spring.io/spring-integration/reference/html/mqtt.html#mqtt"
*/
@Configuration
public class MqttConfig {
private static final String HOST = "tcp://localhost:1883";
private static final String USERNAME = "elex";
private static final String PASSWORD = "test";
private static final String CLIENT_ID = "i-am-a-server";
private static final String TOPIC_FILTER = "#";
private static final String TOPIC_FILTER_2 = "$SYS/#";
private MqttConnectOptions connectOptions() {
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(true);
options.setServerURIs(new String[]{HOST});
options.setKeepAliveInterval(10);
options.setAutomaticReconnect(true);
options.setUserName(USERNAME);
options.setPassword(PASSWORD.toCharArray());
return options;
}
@Bean
public DefaultMqttPahoClientFactory defaultMqttPahoClientFactory() {
DefaultMqttPahoClientFactory clientFactory = new DefaultMqttPahoClientFactory();
clientFactory.setConnectionOptions(connectOptions());
return clientFactory;
}
@Bean
public MessageChannel mqttInputChannel() {
return new DirectChannel();
}
@Bean
public MessageProducer inboundChannel(DefaultMqttPahoClientFactory clientFactory) {
MqttPahoMessageDrivenChannelAdapter adapter =
new MqttPahoMessageDrivenChannelAdapter(HOST, CLIENT_ID, clientFactory,
TOPIC_FILTER, TOPIC_FILTER_2);
adapter.setCompletionTimeout(5000);
adapter.setConverter(new DefaultPahoMessageConverter());
adapter.setQos(1);
adapter.setOutputChannel(mqttInputChannel());
return adapter;
}
@Bean
@ServiceActivator(inputChannel = "mqttInputChannel")
public MessageHandler messageHandler() {
return new MqttMessageHandler();
}
@Bean
public MessageChannel mqttOutboundChannel() {
return new DirectChannel();
}
@Bean
@ServiceActivator(inputChannel = "mqttOutboundChannel")
public MessageHandler mqttOutbound(DefaultMqttPahoClientFactory clientFactory) {
MqttPahoMessageHandler messageHandler =
new MqttPahoMessageHandler(CLIENT_ID + MqttAsyncClient.generateClientId(),
clientFactory);
messageHandler.setAsync(true);
messageHandler.setDefaultQos(1);
return messageHandler;
}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
@Slf4j
public class MqttMessageHandler implements MessageHandler {
/**
* 구독 중인 MQTT 메시지는 여기서 받는다.
*
* @param message
* @throws MessagingException
*/
@Override
public void handleMessage(Message<?> message) throws MessagingException {
String topic = (String) message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC);
log.info("MQTT Rx: {} {}", topic, message.getPayload());
}
}

View File

@@ -0,0 +1,23 @@
/*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import org.springframework.integration.annotation.MessagingGateway;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.handler.annotation.Header;
@MessagingGateway(defaultRequestChannel = "mqttOutboundChannel")
public interface MqttPublisher {
/**
* MQTT 메시지는 여기서 발행한다.
*
* @param topic
* @param payload
*/
void publish(@Header(MqttHeaders.TOPIC) String topic, String payload);
}

View File

@@ -0,0 +1,6 @@
/*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;

View File

@@ -0,0 +1,4 @@
#
# Copyright (c) 2021. Elex. All Rights Reserved.
# https://www.elex-project.com/
#

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2021. Elex. All Rights Reserved.
~ https://www.elex-project.com/
-->
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{15} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>

View File

@@ -0,0 +1,15 @@
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>MQTT Spring-boot Integration Examples</title>
</head>
<body>
<h1>MQTT Spring-boot Integration Examples</h1>
<dl>
<dt>Usage</dt>
<dd><code>/publish?topic=TOPIC&message=MESSAGE</code></dd>
</dl>
<p>Copyright $copy; 2021 Elex Project. All Rights Reserved.</p>
</body>
</html>

View File

@@ -0,0 +1,18 @@
/*
* Copyright (c) 2021. Elex. All Rights Reserved.
* https://www.elex-project.com/
*/
package kr.pe.elex.examples;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ApplicationTests {
@Test
void contextLoads() {
}
}