2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.core.infrastructure.messaging.impl.ws.client;
24 import java.nio.ByteBuffer;
25 import org.onap.policy.apex.core.infrastructure.messaging.MessageListener;
26 import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.RawMessageHandler;
27 import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.messageblock.MessageBlockHandler;
28 import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.messageblock.RawMessageBlock;
29 import org.onap.policy.apex.core.infrastructure.threading.ApplicationThreadFactory;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
34 * The Class InternalMessageBusClient handles the client side of a web socket and handles the callback mechanism used to
35 * receive messages on the web socket.
37 * @author Sajeevan Achuthan (sajeevan.achuthan@ericsson.com)
38 * @param <M> the generic type of message being handled
40 abstract class InternalMessageBusClient<M> extends WebSocketClientImpl {
41 private static final int THREAD_FACTORY_STACK_SIZE = 256;
43 // The logger for this class
44 private static final XLogger LOGGER = XLoggerFactory.getXLogger(InternalMessageBusClient.class);
46 // Name of the event bus.
47 private static final String RAW_EVENT_BUS = "Raw-Event-Bus";
49 // This instance handles the raw data received from the web socket
50 private final RawMessageHandler<M> rawMessageHandler = new RawMessageHandler<>();
52 // The message block handler to which to pass messages coming in on this client
53 private MessageBlockHandler<M> messageBlockHandler = null;
55 // The raw message handler uses a thread to process incoming events off a queue, this class owns and controls that
56 // thread. These fields hold the thread and
57 // the thread factory for creating threads.
58 private ApplicationThreadFactory threadFactory =
59 new ApplicationThreadFactory("ws-client-thread", THREAD_FACTORY_STACK_SIZE);
60 private Thread forwarderThread = null;
63 * Construct the class and start the forwarding thread for received messages.
65 * @param serverUri the server URI to connect to
67 InternalMessageBusClient(final URI serverUri) {
68 // Call the super class to create the web socket
70 LOGGER.entry(serverUri.toString());
72 // Create the data handler for forwarding messages
73 messageBlockHandler = new MessageBlockHandler<>(RAW_EVENT_BUS);
74 messageBlockHandler.registerMessageHandler(rawMessageHandler);
76 // Create the thread that manages the queue in the data handler
77 forwarderThread = threadFactory.newThread(rawMessageHandler);
78 forwarderThread.start();
84 * Callback for binary messages received from the remote host.
86 * @param rawMessage the received raw message
87 * @see org.java_websocket.client.WebSocketClient#onMessage(java.nio.ByteBuffer)
90 public void onMessage(final ByteBuffer rawMessage) {
91 // Post the message to the data handler for forwarding to its listeners
92 messageBlockHandler.post(new RawMessageBlock(rawMessage, null));
96 * Callback for binary messages received from the remote host.
98 * @param stringMessage the string message
99 * @see org.java_websocket.client.WebSocketClient#onMessage(java.lang.String)
102 public final void onMessage(final String stringMessage) {
103 messageBlockHandler.post(stringMessage);
107 * Register a subscriber class to the raw message handler.
109 * @param listener a simple class, that listens for the events from Event
111 public void addMessageListener(final MessageListener<M> listener) {
112 rawMessageHandler.registerDataForwarder(listener);
116 * Removes the message listener.
118 * @param listener the listener
120 public void removeMessageListener(final MessageListener<M> listener) {
121 rawMessageHandler.unRegisterDataForwarder(listener);
125 * Stop the thread handling message forwarding.
127 protected void stopListener() {
128 rawMessageHandler.shutdown();