
	                	                 
Stomp::subscribe 메서드는 Connection 객체 내부에서 구독을 관리합니다. 따라서 Connection을 close() 메서드를 호출해도 구독이 해제되지 않는 이유는 Connection 객체 내부에서 구독이 관리되기 때문입니다.
Stomp::subscribe 메서드를 사용하여 구독을 등록한 후 Connection을 close() 메서드를 호출하는 방법은 없습니다. 왜냐하면 Connection을 close() 메서드를 호출하면 Connection 객체 내부의 모든 리소스가 해제되기 때문입니다.
Connection을 close() 메서드를 호출한 후 Stomp::subscribe 메서드를 다시 호출하여 구독을 등록하는 방법은 다음과 같습니다.
1. Connection을 close() 메서드를 호출하여 Connection 객체 내부의 모든 리소스를 해제합니다.
2. 새로운 Connection 객체를 생성합니다.
3. 새로운 Connection 객체에 Stomp::subscribe 메서드를 호출하여 구독을 등록합니다.
Connection을 close() 메서드를 호출한 후 Stomp::subscribe 메서드를 다시 호출하여 구독을 등록하는 방법의 이점은 다음과 같습니다.
* Connection 객체 내부의 모든 리소스가 해제되므로 메모리 누수나 리소스 누수를 방지할 수 있습니다.
* 새로운 Connection 객체를 생성하여 구독을 등록할 수 있으므로 이전에 등록한 구독이 해제된 후에 새로운 구독을 등록할 수 있습니다.
Connection을 close() 메서드를 호출한 후 Stomp::subscribe 메서드를 다시 호출하여 구독을 등록하는 방법의 단점은 다음과 같습니다.
* Connection을 close() 메서드를 호출하여 Connection 객체 내부의 모든 리소스를 해제하면 Connection 객체를 다시 사용할 수 없으므로 새로운 Connection 객체를 생성해야 합니다.
* 새로운 Connection 객체를 생성하여 구독을 등록할 때 이전에 등록한 구독이 해제된 후에 새로운 구독을 등록하므로 이전에 등록한 구독이 다시 등록되지 않습니다.
Connection을 close() 메서드를 호출한 후 Stomp::subscribe 메서드를 다시 호출하여 구독을 등록하는 방법의 예시는 다음과 같습니다.
#hostingforum.kr
java
// Connection을 close() 메서드를 호출하여 Connection 객체 내부의 모든 리소스를 해제합니다.
connection.close();
// 새로운 Connection 객체를 생성합니다.
Connection newConnection = new Connection();
// 새로운 Connection 객체에 Stomp::subscribe 메서드를 호출하여 구독을 등록합니다.
newConnection.subscribe("topic", new StompListener() {
    @Override
    public void onMessage(StompFrame frame) {
        // 메시지를 처리합니다.
    }
});
Connection을 close() 메서드를 호출한 후 Stomp::subscribe 메서드를 다시 호출하여 구독을 등록하는 방법의 코드 예시는 다음과 같습니다.
#hostingforum.kr
java
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.simp.stomp.StompFrame;
import org.springframework.messaging.simp.stomp.StompListener;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.messaging.WebSocketStompClient;
public class StompClient {
    public static void main(String[] args) {
        // WebSocket 클라이언트를 생성합니다.
        StandardWebSocketClient webSocketClient = new StandardWebSocketClient();
        // WebSocket 클라이언트에 메시지 변환기를 등록합니다.
        MappingJackson2MessageConverter messageConverter = new MappingJackson2MessageConverter();
        WebSocketStompClient stompClient = new WebSocketStompClient(webSocketClient);
        stompClient.setMessageConverter(messageConverter);
        // WebSocket 클라이언트에 연결합니다.
        stompClient.connect("ws://localhost:8080/stomp", new WebSocketHttpHeaders());
        // Connection을 close() 메서드를 호출하여 Connection 객체 내부의 모든 리소스를 해제합니다.
        stompClient.disconnect();
        // 새로운 Connection 객체를 생성합니다.
        stompClient = new WebSocketStompClient(webSocketClient);
        stompClient.setMessageConverter(messageConverter);
        // 새로운 Connection 객체에 Stomp::subscribe 메서드를 호출하여 구독을 등록합니다.
        stompClient.subscribe("topic", new StompListener() {
            @Override
            public void onMessage(StompFrame frame) {
                // 메시지를 처리합니다.
            }
        });
    }
}
Connection을 close() 메서드를 호출한 후 Stomp::subscribe 메서드를 다시 호출하여 구독을 등록하는 방법의 설명은 다음과 같습니다.
* Connection을 close() 메서드를 호출하여 Connection 객체 내부의 모든 리소스를 해제하면 Connection 객체를 다시 사용할 수 없으므로 새로운 Connection 객체를 생성해야 합니다.
* 새로운 Connection 객체를 생성하여 구독을 등록할 때 이전에 등록한 구독이 해제된 후에 새로운 구독을 등록하므로 이전에 등록한 구독이 다시 등록되지 않습니다.
* Connection을 close() 메서드를 호출하여 Connection 객체 내부의 모든 리소스를 해제하면 Connection 객체를 다시 사용할 수 없으므로 새로운 Connection 객체를 생성해야 합니다.
* 새로운 Connection 객체를 생성하여 구독을 등록할 때 이전에 등록한 구독이 해제된 후에 새로운 구독을 등록하므로 이전에 등록한 구독이 다시 등록되지 않습니다.
2025-05-14 21:04