Skip to content

Commit 18dfc4c

Browse files
committed
[grid] Add a command to run the message bus as a standalone component
1 parent bb8e2ce commit 18dfc4c

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.grid.commands;
19+
20+
import com.beust.jcommander.JCommander;
21+
import com.beust.jcommander.ParameterException;
22+
import com.google.auto.service.AutoService;
23+
import com.google.common.collect.ImmutableMap;
24+
import com.google.common.net.MediaType;
25+
import org.openqa.selenium.BuildInfo;
26+
import org.openqa.selenium.cli.CliCommand;
27+
import org.openqa.selenium.events.EventBus;
28+
import org.openqa.selenium.grid.config.AnnotatedConfig;
29+
import org.openqa.selenium.grid.config.CompoundConfig;
30+
import org.openqa.selenium.grid.config.ConcatenatingConfig;
31+
import org.openqa.selenium.grid.config.Config;
32+
import org.openqa.selenium.grid.config.EnvConfig;
33+
import org.openqa.selenium.grid.config.MapConfig;
34+
import org.openqa.selenium.grid.log.LoggingOptions;
35+
import org.openqa.selenium.grid.server.BaseServerFlags;
36+
import org.openqa.selenium.grid.server.BaseServerOptions;
37+
import org.openqa.selenium.grid.server.EventBusFlags;
38+
import org.openqa.selenium.grid.server.EventBusOptions;
39+
import org.openqa.selenium.grid.server.HelpFlags;
40+
import org.openqa.selenium.grid.server.Server;
41+
import org.openqa.selenium.netty.server.NettyServer;
42+
import org.openqa.selenium.remote.http.Contents;
43+
import org.openqa.selenium.remote.http.HttpResponse;
44+
import org.openqa.selenium.remote.http.Route;
45+
46+
import java.util.logging.Logger;
47+
48+
@AutoService(CliCommand.class)
49+
public class MessageBusCommand implements CliCommand {
50+
private static final Logger LOG = Logger.getLogger(MessageBusCommand.class.getName());
51+
52+
@Override
53+
public String getName() {
54+
return "message-bus";
55+
}
56+
57+
@Override
58+
public String getDescription() {
59+
return "Standalone instance of the message bus.";
60+
}
61+
62+
@Override
63+
public boolean isShown() {
64+
return false;
65+
}
66+
67+
@Override
68+
public Executable configure(String... args) {
69+
HelpFlags help = new HelpFlags();
70+
BaseServerFlags baseFlags = new BaseServerFlags(5557);
71+
EventBusFlags eventBusFlags = new EventBusFlags();
72+
73+
JCommander commander = JCommander.newBuilder()
74+
.programName("standalone")
75+
.addObject(baseFlags)
76+
.addObject(eventBusFlags)
77+
.addObject(help)
78+
.build();
79+
80+
return () -> {
81+
try {
82+
commander.parse(args);
83+
} catch (ParameterException e) {
84+
System.err.println(e.getMessage());
85+
commander.usage();
86+
return;
87+
}
88+
89+
if (help.displayHelp(commander, System.out)) {
90+
return;
91+
}
92+
93+
Config config = new CompoundConfig(
94+
new EnvConfig(),
95+
new ConcatenatingConfig("message-bus", '.', System.getProperties()),
96+
new AnnotatedConfig(help),
97+
new AnnotatedConfig(eventBusFlags),
98+
new AnnotatedConfig(baseFlags),
99+
new MapConfig(ImmutableMap.of(
100+
"events", ImmutableMap.of(
101+
"bind", true,
102+
"publish", "tcp://*:4442",
103+
"subscribe", "tcp://*:4443"))));
104+
105+
LoggingOptions loggingOptions = new LoggingOptions(config);
106+
loggingOptions.configureLogging();
107+
108+
EventBusOptions events = new EventBusOptions(config);
109+
// We need this reference to stop the bus being garbage collected. Which would be less than ideal.
110+
EventBus bus = events.getEventBus();
111+
112+
BaseServerOptions serverOptions = new BaseServerOptions(config);
113+
114+
Server<?> server = new NettyServer(
115+
serverOptions,
116+
Route.get("/status").to(() -> req ->
117+
new HttpResponse()
118+
.addHeader("Content-Type", MediaType.JSON_UTF_8.toString())
119+
.setContent(Contents.asJson(ImmutableMap.of("ready", true, "message", "Event bus running")))));
120+
server.start();
121+
122+
BuildInfo info = new BuildInfo();
123+
LOG.info(String.format("Started Selenium message bus %s (revision %s)", info.getReleaseLabel(), info.getBuildRevision()));
124+
125+
// If we exit, the bus goes out of scope, and it's closed
126+
Thread.currentThread().join();
127+
128+
LOG.info("Shutting down: " + bus);
129+
};
130+
}
131+
}

0 commit comments

Comments
 (0)