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.stringmessaging;
23 import com.google.common.eventbus.Subscribe;
27 import org.onap.policy.apex.core.infrastructure.messaging.MessageListener;
28 import org.onap.policy.apex.core.infrastructure.messaging.MessagingException;
29 import org.onap.policy.apex.core.infrastructure.messaging.MessagingService;
30 import org.onap.policy.apex.core.infrastructure.messaging.MessagingServiceFactory;
31 import org.onap.policy.apex.core.infrastructure.messaging.impl.ws.messageblock.MessageBlock;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
36 * This class uses a web socket client to send and receive strings over a web socket.
38 * @author Liam Fallon (liam.fallon@ericsson.com)
40 public class WSStringMessageClient implements WSStringMessager {
41 private static final XLogger LOGGER = XLoggerFactory.getXLogger(WSStringMessageClient.class);
43 // Message service factory and the message service itself
44 private final MessagingServiceFactory<String> factory = new MessagingServiceFactory<>();
45 private MessagingService<String> service = null;
47 // The listener to use for reception of strings
48 private WSStringMessageListener wsStringMessageListener;
50 // Address of the server
51 private final String host;
52 private final int port;
53 private String uriString;
56 * Constructor, define the host and port of the server to connect to.
58 * @param host the host of the server
59 * @param port the port of the server
61 public WSStringMessageClient(final String host, final int port) {
70 * org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageSender#start(org.onap.policy.
71 * apex. core.infrastructure.messaging. stringmessaging.WSStringMessageListener)
74 public void start(final WSStringMessageListener newWsStringMessageListener) throws MessagingException {
75 this.wsStringMessageListener = newWsStringMessageListener;
77 uriString = "ws://" + host + ":" + port;
78 LOGGER.entry("web socket event consumer client to \"" + uriString + "\" starting . . .");
81 service = factory.createClient(new URI(uriString));
82 service.addMessageListener(new WSStringMessageClientListener());
83 service.startConnection();
84 } catch (final Exception e) {
85 LOGGER.warn("web socket event consumer client to \"" + uriString + "\" start failed", e);
86 throw new MessagingException("web socket event consumer client to \"" + uriString + "\" start failed", e);
89 LOGGER.exit("web socket event consumer client to \"" + uriString + "\" started");
95 * @see org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageSender#stop()
99 LOGGER.entry("web socket event consumer client to \"" + uriString + "\" stopping . . .");
100 service.stopConnection();
101 LOGGER.exit("web socket event consumer client to \"" + uriString + "\" stopped");
108 * org.onap.policy.apex.core.infrastructure.messaging.stringmessaging.WSStringMessageSender#sendString(java.lang.
112 public void sendString(final String stringMessage) {
113 service.send(stringMessage);
115 if (LOGGER.isDebugEnabled()) {
116 LOGGER.debug("message sent to server: " + stringMessage);
121 * The Class WSStringMessageClientListener.
123 private class WSStringMessageClientListener implements MessageListener<String> {
127 * @see org.onap.policy.apex.core.infrastructure.messaging.MessageListener#onMessage(org.onap.policy.apex.core.
128 * infrastructure.messaging.impl.ws.messageblock. MessageBlock)
132 public void onMessage(final MessageBlock<String> messageBlock) {
133 throw new UnsupportedOperationException("raw messages are not supported on string message clients");
139 * @see org.onap.policy.apex.core.infrastructure.messaging.MessageListener#onMessage(java.lang.String)
143 public void onMessage(final String messageString) {
144 wsStringMessageListener.receiveString(messageString);