https://github.com/goldmansachs/jrpip
JrPip is a Java Remote Proxy Invocation Provider.
https://github.com/goldmansachs/jrpip
java rmi
Last synced: about 1 year ago
JSON representation
JrPip is a Java Remote Proxy Invocation Provider.
- Host: GitHub
- URL: https://github.com/goldmansachs/jrpip
- Owner: goldmansachs
- License: apache-2.0
- Created: 2017-04-03T17:03:10.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2022-07-24T15:17:02.000Z (almost 4 years ago)
- Last Synced: 2025-03-25T11:11:20.333Z (over 1 year ago)
- Topics: java, rmi
- Language: Java
- Size: 241 KB
- Stars: 44
- Watchers: 13
- Forks: 20
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# JrPip - Java Remote Proxy Invocation Provider
JrPip provides remote method invocation using the Java binary serialization protocol.
The payload is streamed as the objects are being serialized, which significantly improves
speed and reduces memory consumption for large payloads. It can easily handle multi-GB
payloads without multi-GB memory buffers.
## Features
* Streamed serialized payloads. Constant memory usage for all payloads.
* Execute once semantics: temporary network issues are handled transparently without re-executing the remote code.
* No RemoteException. A runtime exception is thrown if the destination becomes completely unreachable during a call.
* Implemented over HTTP in any servlet container.
* Can be encrypted using HTTPS.
* Can use HTTP authentication (cookie or header).
* Proxy/loadbalancing supportable.
* Has a built in socket transport in addition to HTTP
* JrPip is safer than RMI because it never creates classes from the binary payload.
* JrPip does not in any way interfere with garbage collection.
* Binary logging.
* Ability to route to a local implementation (when configured).
* Server side method interceptor.
### Timeout specification
The default timeout is zero (infinite), which is appropriate for heavy workloads
within closely related endpoints (e.g. same application).
The timeout can be specified in 4 ways, with the following precedence:
- Highest precedence: `System.properties`. A timeout can be specified with the full class
name and method signature. Example:
```java
System.setProperty("jrpip.timeout.com.gs.jrpip.Echo.echoAndSleep_java.lang.String_long", "1000");
```
- `@Timeout` annotation on the method in the interface. Example:
```java
@Timeout(timeoutMillis = 10000)
public int someMethod(String val);
```
- `@Timeout` annotation on the interface class. Example:
```java
@Timeout(timeoutMillis = 5000)
public interface ExampleService
```
- Timeout value used in the call to `factory.create`
### Binary Logging:
The following System properties can be used to configure binary logging.
* `jrpip.enableBinaryLogs`: boolean. Default: `false`. Enables binary logging.
* `jrpip.binaryLogsDirectory`: string. Default: `jrpipBinaryLogs`. Directory where binary logs are stored.
The produced log file can be inspected with the utility ListJrpipRequest. Run that command to see the options.
## Usage with a servlet container
1. Create an interface for the service.
2. Create an implementation of that interface. All objects in the implementation method signatures must be serializable and present
on the classpaths of both the client and server.
3. Deploy JrpipServlet in a servlet container with a configuration that binds the interface to the implementation.
4. On the client, get an instance of the interface from the FastServletProxyFactory and call methods on it.
### Example configuration:
One or more interface/implementation pairs can be configured for a given servlet.
#### Typical xml based configuration:
```xml
JrpipServlet
com.gs.jrpip.server.JrpipServlet
serviceInterface.Example
com.example.ExampleService
serviceClass.Example
com.example.ExampleServiceImpl
serviceInterface.FooService
com.example.foo.FooService
serviceClass.FooService
com.example.foo.FooServiceImpl
```
#### Typical Jetty configuration:
```java
Server server = new Server(9001);
ServletHandler handler = new ServletHandler();
server.setHandler(handler);
ServletHolder holder = handler.addServletWithMapping(JrpipServlet.class, "/JrpipServlet");
holder.setInitParameter("serviceInterface.Echo", "com.gs.jrpip.Echo"); // this is the interface
holder.setInitParameter("serviceClass.Echo", "com.gs.jrpip.EchoImpl"); // this is the implementation
server.start();
```
And older versions of Jetty:
```java
HttpServer server = new HttpServer();
HttpContext context = new HttpContext();
context.setContextPath("/");
ServletHandler servletHandler = new ServletHandler();
context.addHandler(servletHandler);
ServletHolder holder = servletHandler.addServlet("JrpipServlet", "/JrpipServlet", "com.gs.jrpip.server.JrpipServlet");
holder.put("serviceInterface.Echo", "com.gs.jrpip.Echo"); // this is the interface
holder.put("serviceClass.Echo", "com.gs.jrpip.EchoImpl"); // this is the implementation
server.addContext(context);
server.start();
```
### Client side usage:
```java
FastServletProxyFactory fspf = new FastServletProxyFactory();
Echo echo = fspf.create(Echo.class, this.getJrpipUrl()); // HTTP url, e.g. http://example.com:8080/JrpipServlet
echo.someMethod();
```
### Using other features:
* Overloaded `create` methods on `FastServletProxyFactory` can be used to specify timeouts. By default, the remote
method can run as long as it needs to.
* For HTTP basic AUTH, use `new FastServletProxyFactory(USER, PASSWORD)`.
* For HTTP cookie handling, use `new FastServletProxyFactory(tokenArray, path, domain)`.
* To register a local implementation, call `JrpipServiceRegistry.getInstance().addServiceForWebApp` or
`JrpipServiceRegistry.getInstance().addServiceForUrl`.
### Connection manager configuration:
The following System properties can be used to configure JrPip (from the java command line):
* `fastServletProxyFactory.maxConnectionsPerHost`: integer. Default: 10. Maximum number of HTTP connections per host.
* `fastServletProxyFactory.maxTotalConnections`: integer. Default `10 * maxConnectionsPerHost`. Maxium number of total connections.
The following static methods on `FastServletProxyFactory` can be used for similar configuration:
* `setMaxConnectionsPerHost`
* `setMaxTotalConnections`
For sticky sessions (typically used with cookies and a loadbalancer), use `SessionAwareFastServletProxyFactory`.
### Method interceptor:
To configure a method interceptor, add a parameter to the servlet configuration:
```xml
methodInterceptor
com.example.foo.FooMethodInterceptor
```
or for Jetty:
```java
holder.put("methodInterceptor", TestMethodInterceptor.class.getName())
```
The method interceptor must implement `com.gs.jrpip.server.MethodInterceptor` and have a no-arg constructor.
See the javadoc in `com.gs.jrpip.server.MethodInterceptor` for call semantics.
### VM Bound configuration:
In some cases, usually when the service implementation is stateful in some way, it is desirable to
disallow the client from connecting to a new instance of the server. To configure such a service,
in the servlet configuration, replace "serviceClass" with "vmBoundServiceClass".
Example:
```xml
JrpipServlet
com.gs.jrpip.server.JrpipServlet
serviceInterface.Example
com.example.ExampleService
vmBoundServiceClass.Example
com.example.ExampleServiceImpl
```
## Usage with socket transport
1. Create an interface for the service.
2. Create an implementation of that interface. All objects in the implementation method signatures must be serializable and present
on the classpaths of both the client and server.
3. Deploy a `SocketServer` configured via `SocketServerConfig`.
4. On the client, get an instance of the interface from the `MtProxyFactory` and call methods on it.
### Example configuration:
One or more interface/implementation pairs can be configured for a given server.
See the javadoc for `SocketServerConfig` for more options.
```java
SocketServerConfig config = new SocketServerConfig(9001);
config.addServiceConfig(ExampleService.class, ExampleServiceImpl.class);
config.addServiceConfig(AnotherService.class, AnotherServiceImpl.class);
SocketServer server = new SocketServer(config);
server.start();
```
### Client side usage:
```java
SocketMessageTransport transport = new SocketMessageTransport();
MtProxyFactory factory = new MtProxyFactory(transport);
ExampleService example = factory.create(ExampleService.class,
"jpfs://localhost:9001"); // URL for socket server is always jpfs://:
```
### Authentication
The socket server can be secured with a username/token pair (or pairs). The authentication
sends the username and a hashed challenge (nonce), so the token is never sent over the wire.
A socket is only authenticated once, right after connection.
On the server side:
```java
config.addCredentials("fred", "lkjhhjas56786349873dliuonkje");
```
and on the client:
```java
SocketMessageTransport transport = new SocketMessageTransport("fred", "lkjhhjas56786349873dliuonkje");
```
## Encryption
Encryption (AES-128/CBC) can be enabled when constructing the client side transport.
```java
SocketMessageTransport transport = new SocketMessageTransport("fred", "lkjhhjas56786349873dliuonkje", true);
```
Encryption requires authentication, as it derives a per-session key from
the user token and per-session nonce.
### Method interceptor:
See the javadoc for `SocketServerConfig` and `MethodInterceptor`
### Event Listener
See the javadoc for `SocketServerConfig` and `JrpipEventListener`
### VM Bound configuration:
In some cases, usually when the service implementation is stateful in some way, it is desirable to
disallow the client from connecting to a new instance of the server. To configure such a service,
add a vmbound (boolean) to the service configuration call:
```java
config.addServiceConfig(ExampleService.class, ExampleServiceImpl.class, true);
```
### Compression
The socket based transport can additionally turn off compression via annotations.
`@Compression(compress = false)` can be specified at the interface class level, or
at the method level, with the method level overriding the class level.
The LZ4 compression used in JrPip is very fast/light and generally there is no
benefit in changing it.