(32109) - java.io.EOFException

공부는 혼자하는 거·2022년 3월 23일
0

에러모음

목록 보기
7/28
연결 유실 (32109) - java.io.EOFException
	at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:190)
	at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.io.EOFException
	at java.base/java.io.DataInputStream.readByte(DataInputStream.java:272)
	at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:92)
	at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:137)
	... 1 more

TB에서 똑같은 Publish-id로 물고있어서 에러.. publish Id를 서버별로 다르게 설정하는 걸로 해결..
또 Publish-id 를 소문자 - 소문자 로 하면 에러가 뜨는 것 같다.. 네이밍 규칙이 따로 있는듯..


import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Slf4j
@Component
public class MyMqttClient implements MqttCallback {

    /**
     * mqtt.ekg.kr:1883
     * 
     * MQTT_PUBLISHER_ID를 yangjae-server 에서 yangjae_server로 변경
     * 
     */
    private final String MQTT_PUBLISHER_ID;
    private final String MQTT_SERVER_ADDRES;
    private MqttClient instance;

//    String tenantId = "DEFAULT_TENANT";
//    String authId = "sensor1";
//    String username = authId+"@"+tenantId;
    MqttConnectOptions options = new MqttConnectOptions();

    public MyMqttClient(@Value("${mqtt.MQTT_PUBLISHER_ID}") String mqtt_publisher_id, @Value("${mqtt.MQTT_SERVER_ADDRES}") String MQTT_SERVER_ADDRES) {
        this.MQTT_PUBLISHER_ID = mqtt_publisher_id;
        this.MQTT_SERVER_ADDRES = MQTT_SERVER_ADDRES;
    }

//    public MyMqttClient( @Value("${mqtt.MQTT_SERVER_ADDRES}") String MQTT_SERVER_ADDRES) {
//        this.MQTT_SERVER_ADDRES = MQTT_SERVER_ADDRES;
//    }

    public MqttClient getInstance() {
        return instance;
    }


    //todo 굳이 멀티스레드 문제를 신경쓸 필요없을텐데? 이거는 첫바따로 실행되니까.

    @PostConstruct
    public void init() {
        if (instance == null) {
            connect();
        }
    }


    protected void connect() {
        try {

            log.info(MQTT_SERVER_ADDRES + " mqtt 서버 접속..");
            instance = new MqttClient(MQTT_SERVER_ADDRES, MQTT_PUBLISHER_ID);
            instance.setCallback(this);

            options.setAutomaticReconnect(true); //이거 자체만으로는 불가능할까?
            options.setCleanSession(true);
            options.setConnectionTimeout(30);



            try {
                if (!instance.isConnected()) {
                    log.info("mqtt 서버에 접속이 안 되어있다면..");
                    instance.connect(options);
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        } catch (MqttException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void connectionLost(Throwable throwable) {
        throwable.printStackTrace();

        log.info("disconnect => " +  throwable);
    }

    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
        log.info("messageArrived===" + s + " , " + mqttMessage);
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

        log.info("deliveryComplete");
    }
}


참고

https://stackoverflow.com/questions/63589919/java-io-eofexception-32109-when-running-eclipse-paho-simulator-with-example-ho
https://www.eclipse.org/hono/docs/user-guide/mqtt-adapter/
https://stackoverflow.com/questions/46788749/connection-lost-32109-java-io-eofexception

profile
시간대비효율

0개의 댓글