https://github.com/dinstone/photon
photon is a message exchange framework
https://github.com/dinstone/photon
Last synced: about 2 months ago
JSON representation
photon is a message exchange framework
- Host: GitHub
- URL: https://github.com/dinstone/photon
- Owner: dinstone
- License: apache-2.0
- Created: 2018-10-08T12:39:30.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2025-02-10T17:40:54.000Z (10 months ago)
- Last Synced: 2025-10-11T04:54:46.560Z (about 2 months ago)
- Language: Java
- Homepage:
- Size: 323 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Photon
[](https://github.com/dinstone/photon/blob/master/LICENSE)
[](https://search.maven.org/search?q=com.dinstone.photon)
# Overview
**Photon** is an asynchronous message exchange framework based on Netty.
# Features
* Efficient custom protocol ([Photon](https://github.com/dinstone/photon) message exchange protocol)
- Request-Response Pattern
- One-way / Notify Pattern
* High-performance NIO socket framework support - Netty4
# Quick Start
## add dependency:
```xml
com.dinstone.photon
photon
1.2.4
```
## message provider:
```java
public static void main(String[] args) throws Exception {
AcceptOptions acceptOptions = new AcceptOptions();
acceptOptions.setEnableSsl(true);
acceptOptions.setIdleTimeout(60000);
SelfSignedCertificate cert = new SelfSignedCertificate();
acceptOptions.setPrivateKey(cert.key());
acceptOptions.setCertChain(new X509Certificate[] { cert.cert() });
Acceptor acceptor = new Acceptor(acceptOptions);
acceptor.setProcessor(new MessageProcessor() {
@Override
public void process(Connection connection, Request req) {
LOG.info("Request is {}", req.getSequence());
Notice notice = new Notice();
notice.setAddress("order.created");
notice.setContent(req.getContent());
CompletableFuture f = connection.sendMessage(notice);
f.thenAccept((v) -> {
Response response = new Response();
response.setSequence(req.getSequence());
response.setStatus(Status.SUCCESS);
response.setContent(req.getContent());
connection.sendMessage(response);
});
}
});
acceptor.bind(new InetSocketAddress("127.0.0.1", 4444));
System.in.read();
acceptor.destroy().awaitUninterruptibly();
}
```
## message consumer:
```java
public static void main(String[] args) throws Throwable {
ConnectOptions connectOptions = new ConnectOptions();
connectOptions.setEnableSsl(true);
Connector connector = new Connector(connectOptions);
Connection connection = connector.connect(new InetSocketAddress("127.0.0.1", 4444));
LOG.info("channel active is {}", connection.isActive());
Request request = new Request();
request.setSequence(1);
request.setTimeout(10000);
request.setContent("Hello World".getBytes());
LOG.info("async request is {}", request);
connection.sendRequest(request).thenAccept(response -> {
LOG.info("async response is {}", response);
});
request = new Request();
request.setSequence(2);
request.setTimeout(3000);
LOG.info("sync request is {}", request);
Response response = connection.sendRequest(request).get();
LOG.info("sync response is {}", response);
System.in.read();
connector.destroy().awaitUninterruptibly();
}
```