com.codeveo.lago.bot.stomp.client.LagoStompMessageHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.codeveo.lago.bot.stomp.client.LagoStompMessageHandler.java

Source

/*
 * Copyright 2014 Ladislav Klenovic <klenovic@nerdrobot.net>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.codeveo.lago.bot.stomp.client;

import java.util.concurrent.BlockingQueue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.messaging.Message;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;

import com.codeveo.lago.common.shared.WebServiceUserDesc;
import com.codeveo.lago.security.management.rest.api.RLagoBot;

public class LagoStompMessageHandler implements StompMessageHandler {
    private static final Logger LOG = LoggerFactory.getLogger(LagoStompMessageHandler.class);
    private BlockingQueue<Message<byte[]>> queue;
    private RLagoBot lagoBot;
    private WebSocketStompSession stompSession;

    public LagoStompMessageHandler(RLagoBot lagoBot, BlockingQueue<Message<byte[]>> queue) {
        this.lagoBot = lagoBot;
        this.queue = queue;
    }

    @Override
    public void afterConnected(WebSocketStompSession stompSession, StompHeaderAccessor headers) {
        this.stompSession = stompSession;
        stompSession.subscribe(WebServiceUserDesc.TOPIC_BROADCAST_SUBSCRIPTION_PATH,
                lagoBot.getUsername() + "-broadcast");
        stompSession.subscribe(WebServiceUserDesc.TOPIC_PRIVATE_SUBSCRIPTION_PATH, null);
    }

    @Override
    public void handleReceipt(String receiptId) {
        LOG.info("Got receipt '{}'", receiptId);
    }

    @Override
    public void handleMessage(Message<byte[]> message) {
        /* Do nothing more than put the message into the queue. Handle it in dedicated thread. */
        boolean added = queue.offer(message);

        if (!added) {
            LOG.error("Message listener was unable to add messsage into the message queue!");
        }
    }

    @Override
    public void handleError(Message<byte[]> message) {
        LOG.error("Error occured: '{}'", message.getPayload());
    }

    @Override
    public void afterDisconnected() {
        LOG.info("Disconnected in '{}'", this.stompSession);
    }

}