Skip to content

Commit

Permalink
Add a session reconnect method.
Browse files Browse the repository at this point in the history
  • Loading branch information
broneill committed Oct 6, 2024
1 parent 3e728ea commit e563b5c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ v2.4.0
* Ignore static methods in remote interfaces.
* Added pipe methods for efficiently encoding and decoding complex objects.
* Added support for automatic remote object disposal.
* Added a session reconnect method.
* Optimize reading and writing primitive arrays.

v2.3.3 (2024-04-10)
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/cojen/dirmi/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ void connected(SocketAddress localAddr, SocketAddress remoteAddr,
*/
void addStateListener(BiPredicate<Session<?>, Throwable> listener);

/**
* Closes all connections and initiates a reconnect.
*/
void reconnect();

/**
* Closes all connections and immediately closes any future connections. All remote objects
* are invalidated as a side effect.
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/cojen/dirmi/core/CoreSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,11 @@ private void notifyListeners(Throwable ex) {
}
}

@Override
public final void reconnect() {
close(R_DISCONNECTED, null);
}

@Override
public final void close() {
close(R_CLOSED, null);
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/org/cojen/dirmi/RestorableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,51 @@ public boolean test(Session<?> session, Throwable ex) {
assertEquals(List.of(Session.State.CLOSED), listener.states);
}

@Test
public void explicitReconnect() throws Exception {
assertEquals(Session.State.CONNECTED, mSession.state());

R1 root = mSession.root();
R2 r2 = root.a(123);

R1 r1x = r2.a();

assertEquals("hello", r1x.echo("hello"));

var listener = new BiPredicate<Session<?>, Throwable>() {
volatile boolean reconnected;

@Override
public boolean test(Session<?> session, Throwable ex) {
if (session.state() == Session.State.RECONNECTED) {
reconnected = true;
}
return true;
}
};


mSession.addStateListener(listener);

mSession.reconnect();

check: {
for (int i=0; i<100; i++) {
try {
assertEquals("world", r1x.echo("world"));
break check;
} catch (DisconnectedException e) {
}

Thread.sleep(100);
}

fail("Not restored: " + mSession.state());
}

assertTrue(listener.reconnected);
}

@Test
public void dispose() throws Exception {
// A disposed object cannot be restored, and neither can any objects that depend on it
Expand Down

0 comments on commit e563b5c

Please sign in to comment.