org.openqa.selenium.events.zeromq.UnboundZmqEventBus Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of selenium-grid Show documentation
Show all versions of selenium-grid Show documentation
Selenium automates browsers. That's it! What you do with that power is entirely up to you.
The newest version!
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you 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 org.openqa.selenium.events.zeromq;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.common.collect.EvictingQueue;
import dev.failsafe.Failsafe;
import dev.failsafe.RetryPolicy;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Queue;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.events.Event;
import org.openqa.selenium.events.EventBus;
import org.openqa.selenium.events.EventListener;
import org.openqa.selenium.events.EventName;
import org.openqa.selenium.grid.security.Secret;
import org.openqa.selenium.internal.Require;
import org.openqa.selenium.json.Json;
import org.openqa.selenium.json.JsonException;
import org.openqa.selenium.json.JsonOutput;
import org.zeromq.SocketType;
import org.zeromq.ZContext;
import org.zeromq.ZMQ;
class UnboundZmqEventBus implements EventBus {
static final EventName REJECTED_EVENT = new EventName("selenium-rejected-event");
private static final Logger LOG = Logger.getLogger(EventBus.class.getName());
private static final Json JSON = new Json();
private final AtomicBoolean pollingStarted = new AtomicBoolean(false);
private final ExecutorService socketPollingExecutor;
private final ExecutorService socketPublishingExecutor;
private final ExecutorService listenerNotificationExecutor;
private final Map>> listeners = new ConcurrentHashMap<>();
private final Queue recentMessages = EvictingQueue.create(128);
private final String encodedSecret;
private final ZMQ.Poller poller;
private ZMQ.Socket pub;
private ZMQ.Socket sub;
UnboundZmqEventBus(
ZContext context, String publishConnection, String subscribeConnection, Secret secret) {
Require.nonNull("Secret", secret);
StringBuilder builder = new StringBuilder();
try (JsonOutput out = JSON.newOutput(builder)) {
out.setPrettyPrint(false).writeClassName(false).write(secret);
}
this.encodedSecret = builder.toString();
this.socketPollingExecutor =
Executors.newSingleThreadExecutor(
r -> {
Thread thread = new Thread(r);
thread.setName("Event Bus Poller");
thread.setDaemon(true);
return thread;
});
this.socketPublishingExecutor =
Executors.newSingleThreadExecutor(
r -> {
Thread thread = new Thread(r);
thread.setName("Event Bus Publisher");
thread.setDaemon(true);
return thread;
});
this.listenerNotificationExecutor =
Executors.newFixedThreadPool(
Math.max(Runtime.getRuntime().availableProcessors() / 2, 2), // At least two threads
r -> {
Thread thread = new Thread(r);
thread.setName("Event Bus Listener Notifier");
thread.setDaemon(true);
return thread;
});
String connectionMessage =
String.format("Connecting to %s and %s", publishConnection, subscribeConnection);
LOG.info(connectionMessage);
RetryPolicy
© 2015 - 2024 Weber Informatics LLC | Privacy Policy