Skip to content

Commit 339c8ae

Browse files
fipro78dirkfauth
authored andcommitted
Impl #44 - Add CRaC support
1 parent 1fef9eb commit 339c8ae

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

org.eclipse.osgitech.rest.jetty/pom.xml

+25
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@
7474
<groupId>org.apache.felix</groupId>
7575
<artifactId>org.apache.felix.http.jetty</artifactId>
7676
</dependency>
77+
<dependency>
78+
<groupId>org.crac</groupId>
79+
<artifactId>crac</artifactId>
80+
</dependency>
7781
</dependencies>
7882

7983
<build>
@@ -117,6 +121,27 @@
117121
</execution>
118122
</executions>
119123
</plugin>
124+
<plugin>
125+
<groupId>biz.aQute.bnd</groupId>
126+
<artifactId>bnd-maven-plugin</artifactId>
127+
<extensions>true</extensions>
128+
<executions>
129+
<execution>
130+
<id>jar</id>
131+
<goals>
132+
<goal>jar</goal>
133+
</goals>
134+
<configuration>
135+
<bnd><![CDATA[
136+
Import-Package: org.crac.*;resolution:=optional, *
137+
-noextraheaders: true
138+
-noimportjava: true
139+
-fixupmessages: The JAR is empty:
140+
]]></bnd>
141+
</configuration>
142+
</execution>
143+
</executions>
144+
</plugin>
120145
<plugin>
121146
<groupId>biz.aQute.bnd</groupId>
122147
<artifactId>bnd-resolver-maven-plugin</artifactId>

org.eclipse.osgitech.rest.jetty/src/main/java/org/eclipse/osgitech/rest/jetty/JettyBackedWhiteboardComponent.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright (c) 2012 - 2022 Data In Motion and others.
2+
* Copyright (c) 2012 - 2024 Data In Motion and others.
33
* All rights reserved.
44
*
55
* This program and the accompanying materials are made available under the terms of the
@@ -10,6 +10,7 @@
1010
* Data In Motion - initial API and implementation
1111
* Stefan Bishof - API and implementation
1212
* Tim Ward - implementation
13+
* Dirk Fauth - add CRaC support
1314
*/
1415
package org.eclipse.osgitech.rest.jetty;
1516

@@ -62,6 +63,10 @@ public class JettyBackedWhiteboardComponent {
6263
Logger logger = Logger.getLogger(JettyBackedWhiteboardComponent.class.getName());
6364

6465
private JerseyServiceRuntime<WhiteboardServletContainer> serviceRuntime;
66+
67+
// need to keep a strong reference to avoid that the resource gets garbage collected
68+
@SuppressWarnings("unused")
69+
private JettyCracResource cracHandler;
6570

6671
public enum State {
6772
INIT, STARTED, STOPPED, EXCEPTION
@@ -93,6 +98,17 @@ public void activate(BundleContext context, Map<String, Object> properties) thro
9398

9499

95100
serviceRuntime.start(getServiceRuntimeProperties(properties));
101+
102+
try {
103+
Class.forName("org.crac.Resource");
104+
105+
// org.crac.Resource was found, so we create an instance of the JettyCracResource
106+
cracHandler = new JettyCracResource(this);
107+
} catch (ClassNotFoundException e) {
108+
// org.crac.Resource could not be found
109+
// we simply do nothing, as CRaC support is not available
110+
}
111+
96112
}
97113

98114
private Map<String, Object> getServiceRuntimeProperties(Map<String, Object> properties) {
@@ -348,4 +364,12 @@ private void stopServer() {
348364
logger.log(Level.SEVERE, "Error stopping Jetty server", e);
349365
}
350366
}
367+
368+
/**
369+
*
370+
* @return the Jetty server managed by this class. Can be <code>null</code>.
371+
*/
372+
Server getJettyServer() {
373+
return jettyServer;
374+
}
351375
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright (c) 2024 Dirk Fauth and others.
3+
* All rights reserved.
4+
*
5+
* This program and the accompanying materials are made available under the terms of the
6+
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
7+
* http://www.eclipse.org/legal/epl-v20.html
8+
*
9+
* Contributors:
10+
* Dirk Fauth - initial API and implementation
11+
*/
12+
package org.eclipse.osgitech.rest.jetty;
13+
14+
import java.util.Arrays;
15+
16+
import org.crac.Context;
17+
import org.crac.Core;
18+
import org.crac.Resource;
19+
import org.eclipse.jetty.server.Server;
20+
import org.eclipse.jetty.util.component.LifeCycle;
21+
22+
/**
23+
* This class is used to add CRaC support to the jetty bundle by using the <code>org.crac</code> API.
24+
* It adapts the examples and best practices from the CRaC documentation and the examples.
25+
* @see <a href="https://github.com/CRaC/org.crac">`org.crac` API</a>
26+
* @see <a href="https://docs.azul.com/core/crac/crac-tips-tricks#implementing-resource-as-inner-class">Implementing Resource as Inner Class</a>
27+
* @see <a href="https://github.com/CRaC/docs/blob/master/STEP-BY-STEP.md">Step-by-step CRaC support for a Jetty app</a>
28+
* @see <a href="https://github.com/CRaC/example-jetty/blob/master/src/main/java/com/example/App.java">CRaC example-jetty</a>
29+
*/
30+
public class JettyCracResource {
31+
32+
// the org.crac.Resource is implemented as an inner class and kept as a strong reference
33+
// to avoid that it is garbage collected after the registration.
34+
private Resource cracHandler;
35+
36+
public JettyCracResource(JettyBackedWhiteboardComponent jettyComponent) {
37+
cracHandler = new Resource() {
38+
@Override
39+
public void beforeCheckpoint(Context<? extends Resource> context) {
40+
Server jettyServer = jettyComponent.getJettyServer();
41+
if (jettyServer != null && !jettyServer.isStopped()) {
42+
// Stop the connectors only and keep the expensive application running
43+
Arrays.asList(jettyServer.getConnectors()).forEach(c -> LifeCycle.stop(c));
44+
}
45+
}
46+
47+
@Override
48+
public void afterRestore(Context<? extends Resource> context) {
49+
Server jettyServer = jettyComponent.getJettyServer();
50+
if (jettyServer != null && !jettyServer.isStopped()) {
51+
Arrays.asList(jettyServer.getConnectors()).forEach(c -> LifeCycle.start(c));
52+
}
53+
}
54+
};
55+
Core.getGlobalContext().register(cracHandler);
56+
}
57+
}

pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<asm.version>9.5</asm.version>
7070
<jackson.version>2.15.2</jackson.version>
7171
<parsson.version>1.1.5</parsson.version>
72+
<crac.version>1.5.0</crac.version>
7273
</properties>
7374

7475
<modules>
@@ -464,6 +465,12 @@
464465
<artifactId>biz.aQute.bnd.annotation</artifactId>
465466
<version>6.4.0</version>
466467
</dependency>
468+
<dependency>
469+
<groupId>org.crac</groupId>
470+
<artifactId>crac</artifactId>
471+
<version>${crac.version}</version>
472+
<scope>provided</scope>
473+
</dependency>
467474
</dependencies>
468475
</dependencyManagement>
469476

0 commit comments

Comments
 (0)