Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -412,13 +412,15 @@ private void enableJmx(Server server) {
}
}

private void enableSessionSupport(Server server, String connectorKey) {
private void enableSessionSupport(Server server, String connectorKey) throws Exception {
ServletContextHandler context = server.getDescendant(ServletContextHandler.class);
if (context.getSessionHandler() == null) {
SessionHandler sessionHandler = new SessionHandler();
if (context.isStarted()) {
throw new IllegalStateException(
"Server has already been started. Cannot enabled sessionSupport on " + connectorKey);
LOG.debug("Restarting Jetty server to enable session support on {}", connectorKey);
server.stop();
context.setSessionHandler(sessionHandler);
server.start();
} else {
context.setSessionHandler(sessionHandler);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
*/
package org.apache.camel.component.jetty;

import java.util.HashSet;
import java.util.Set;

import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.AvailablePortFinder;
import org.apache.camel.test.junit6.CamelTestSupport;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class JettyFailoverRoundRobinTest extends CamelTestSupport {

Expand All @@ -39,27 +42,23 @@ public class JettyFailoverRoundRobinTest extends CamelTestSupport {

@Test
void testJettyFailoverRoundRobin() throws Exception {
getMockEndpoint("mock:bad").expectedMessageCount(1);
getMockEndpoint("mock:bad2").expectedMessageCount(1);
getMockEndpoint("mock:good").expectedMessageCount(1);
getMockEndpoint("mock:good2").expectedMessageCount(0);

String reply = template.requestBody("direct:start", null, String.class);
assertEquals("Good", reply);

MockEndpoint.assertIsSatisfied(context);

// reset mocks and send a message again to see that round robin
// continue where it should
MockEndpoint.resetMocks(context);
// Send two requests through the failover round-robin load balancer.
// The round-robin starting index is not guaranteed, so we verify
// that both good endpoints are reached across the two requests
// (one each), confirming both failover and round-robin behavior.
String reply1 = template.requestBody("direct:start", null, String.class);
assertTrue("Good".equals(reply1) || "Also good".equals(reply1),
"Expected 'Good' or 'Also good' but was: " + reply1);

getMockEndpoint("mock:bad").expectedMessageCount(0);
getMockEndpoint("mock:bad2").expectedMessageCount(0);
getMockEndpoint("mock:good").expectedMessageCount(0);
getMockEndpoint("mock:good2").expectedMessageCount(1);
String reply2 = template.requestBody("direct:start", null, String.class);
assertTrue("Good".equals(reply2) || "Also good".equals(reply2),
"Expected 'Good' or 'Also good' but was: " + reply2);

reply = template.requestBody("direct:start", null, String.class);
assertEquals("Also good", reply);
Set<String> replies = new HashSet<>();
replies.add(reply1);
replies.add(reply2);
assertEquals(2, replies.size(),
"Round robin should hit both good endpoints, but got: " + reply1 + " and " + reply2);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class JettySessionSupportTest extends BaseJettyTest {

Expand All @@ -31,22 +30,24 @@ void setup() {
}

@Test
void testJettySessionSupportInvalid() {
RouteBuilder routeBuilder = new RouteBuilder() {
void testJettySessionSupportOnExistingServer() throws Exception {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() {
from("jetty:http://localhost:{{port}}/hello").to("mock:foo");
from("jetty:http://localhost:{{port}}/hello").transform(simple("Hello ${body}"));

from("jetty:http://localhost:{{port}}/bye?sessionSupport=true").to("mock:bar");
from("jetty:http://localhost:{{port}}/bye?sessionSupport=true").transform(simple("Bye ${body}"));
}
};
});
context.start();

assertThrows(
IllegalStateException.class,
() -> context.addRoutes(routeBuilder),
"Server has already been started. Cannot enabled sessionSupport on http:localhost:%d".formatted(getPort()));
try {
String reply = template.requestBody("http://localhost:{{port}}/hello", "World", String.class);
assertEquals("Hello World", reply);

if (context.isStarted()) {
reply = template.requestBody("http://localhost:{{port}}/bye", "World", String.class);
assertEquals("Bye World", reply);
} finally {
context.stop();
}
}
Expand Down
Loading