forked from Linyuzai/concept
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
新增(Servlet/Reactive)WebSocketClientFactory
- Loading branch information
tanghanzheng
committed
Oct 8, 2023
1 parent
4b429f8
commit 9bda3b5
Showing
11 changed files
with
144 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...uzai/connection/loadbalance/websocket/reactive/DefaultReactiveWebSocketClientFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.github.linyuzai.connection.loadbalance.websocket.reactive; | ||
|
||
import com.github.linyuzai.connection.loadbalance.websocket.concept.WebSocketLoadBalanceException; | ||
import lombok.SneakyThrows; | ||
import org.springframework.util.ClassUtils; | ||
import org.springframework.web.reactive.socket.client.*; | ||
import org.xnio.OptionMap; | ||
import org.xnio.Xnio; | ||
import org.xnio.XnioWorker; | ||
|
||
public class DefaultReactiveWebSocketClientFactory implements ReactiveWebSocketClientFactory { | ||
|
||
private static final boolean tomcatPresent; | ||
|
||
private static final boolean jettyPresent; | ||
|
||
private static final boolean undertowPresent; | ||
|
||
private static final boolean reactorNettyPresent; | ||
|
||
static { | ||
ClassLoader loader = ReactiveWebSocketConnectionSubscriber.class.getClassLoader(); | ||
tomcatPresent = ClassUtils.isPresent("org.apache.tomcat.websocket.WsWebSocketContainer", loader); | ||
jettyPresent = ClassUtils.isPresent("org.eclipse.jetty.websocket.client.WebSocketClient", loader); | ||
undertowPresent = ClassUtils.isPresent("io.undertow.websockets.core.WebSocketChannel", loader); | ||
reactorNettyPresent = ClassUtils.isPresent("reactor.netty.http.websocket.WebsocketInbound", loader); | ||
} | ||
|
||
@SneakyThrows | ||
@Override | ||
public WebSocketClient create() { | ||
if (reactorNettyPresent) { | ||
return new ReactorNettyWebSocketClient(); | ||
} else if (undertowPresent) { | ||
XnioWorker worker = Xnio.getInstance().createWorker(OptionMap.builder().getMap()); | ||
return new UndertowWebSocketClient(worker); | ||
} else if (jettyPresent) { | ||
return new JettyWebSocketClient(); | ||
} else if (tomcatPresent) { | ||
return new TomcatWebSocketClient(); | ||
} else { | ||
throw new WebSocketLoadBalanceException("No suitable client found"); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ub/linyuzai/connection/loadbalance/websocket/reactive/ReactiveWebSocketClientFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.github.linyuzai.connection.loadbalance.websocket.reactive; | ||
|
||
import org.springframework.web.reactive.socket.client.WebSocketClient; | ||
|
||
public interface ReactiveWebSocketClientFactory { | ||
|
||
WebSocketClient create(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...nyuzai/connection/loadbalance/websocket/servlet/DefaultServletWebSocketClientFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.github.linyuzai.connection.loadbalance.websocket.servlet; | ||
|
||
import org.springframework.util.ClassUtils; | ||
import org.springframework.web.socket.client.WebSocketClient; | ||
import org.springframework.web.socket.client.jetty.JettyWebSocketClient; | ||
import org.springframework.web.socket.client.standard.StandardWebSocketClient; | ||
|
||
public class DefaultServletWebSocketClientFactory implements ServletWebSocketClientFactory { | ||
|
||
private static final boolean jettyPresent; | ||
|
||
static { | ||
ClassLoader loader = ServletWebSocketConnectionSubscriber.class.getClassLoader(); | ||
jettyPresent = ClassUtils.isPresent("org.eclipse.jetty.websocket.client.WebSocketClient", loader); | ||
} | ||
|
||
@Override | ||
public WebSocketClient create() { | ||
if (jettyPresent) { | ||
return new JettyWebSocketClient(); | ||
} else { | ||
return new StandardWebSocketClient(); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...thub/linyuzai/connection/loadbalance/websocket/servlet/ServletWebSocketClientFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.github.linyuzai.connection.loadbalance.websocket.servlet; | ||
|
||
import org.springframework.web.socket.client.WebSocketClient; | ||
|
||
public interface ServletWebSocketClientFactory { | ||
|
||
WebSocketClient create(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.