Class CloudEdgeSwitch
- All Implemented Interfaces:
Command<ChenileExchange>
This interceptor, if configured, acts like an edge switch for a service i.e. it turns a service from a cloud service into an edge service.
This switch looks for a property called remoteUrl. If the remote Url is not configured then it assumes that the service is running in the cloud. For the cloud it merely passes control to the service and publishes a message upon successful execution so that all edges can update themselves
If the remoteUrl is configured then it switches the functionality to the "edge" mode.
The edge service logic is as follows:
* If the cloud service is detected and can be successfully invoked synchronously via HTTP, then the edge service is a
"forward only" service. Else it is a "store and forward service"
The local service call is bypassed completely at the edge. The Switch attempts to call the same service but hosted in the cloud. The remote url is the cloud location of the service. If the cloud call is successful, then this effectively acts as if the edge service is a traffic cop merely directing all the incoming traffic to the cloud and returning the result of the cloud service call. This is the 'forward only' functionality
If the call fails due to a problem with the connection (we check this explicitly) then the "store and forward" part of the logic kicks in. In this case the local service is called. If successful, the result of the local service call is returned with a warning stating that the transaction succeeded but it is done locally due to network failure.
Then a message is forwarded to the cloud via MQ-TT. This message, when received, will cause the cloud service to update itself and also notify all the edges (except the edge that notified the cloud in the first place)
-
Field Summary
Modifier and TypeFieldDescriptionprivate String
private HeaderCopier
(package private) org.slf4j.Logger
private MqttPublisher
(package private) ObjectWriter
private ProxyBuilder
private String
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionaddMqttTargetHeaderIfRequired
(ChenileExchange exchange) If this message came from MQTT then the original source must not be receiving this message since that would create duplicates.protected boolean
bypassInterception
(ChenileExchange exchange) Over-ride this to bypass interception in special circumstancesprivate boolean
callCloud
(ChenileExchange exchange) Switch to cloud proxy and invoke it via httpprivate boolean
callEdge
(ChenileExchange exchange, Object serviceReference, ChainContext.SavePoint savePoint) private void
enhanceWarnings
(ErrorNumException exception, ChenileExchange exchange) void
execute
(ChenileExchange exchange) If this method is over-ridden, make sure that you call doContinue unless you want to end the chain here! This is very important.private Object
getRemoteProxy
(ChenileExchange exchange) private void
handleCloud
(ChenileExchange exchange) In the cloud, execute the actual service Upon successful return, send a message to MQTT topicprivate void
handleEdge
(ChenileExchange exchange) In the edge, we will delegate the control to the cloud.private void
handleHttp
(ChenileExchange exchange) private void
handleMqtt
(ChenileExchange exchange) In MQTT, if the message comes to the edge, call the service as usual If it is the cloud, handle it like you do HTTP messagesprivate void
publishMessage
(ChenileExchange exchange, Map<String, Object> headers) void
setCloudClientId
(String cloudClientID) void
setRemoteUrl
(String remoteUrl) private void
switchServiceReference
(Object ref, ChenileExchange exchange) private String
Methods inherited from class org.chenile.core.interceptors.BaseChenileInterceptor
doContinue, doPostProcessing, doPreProcessing, getExtensionByAnnotation, getExtensionByAnnotation, resumeFromSavedPoint, savePoint
-
Field Details
-
logger
org.slf4j.Logger logger -
remoteUrl
-
cloudClientID
-
mqttPublisher
-
proxyBuilder
-
headerCopier
-
objectWriter
ObjectWriter objectWriter
-
-
Constructor Details
-
CloudEdgeSwitch
public CloudEdgeSwitch()
-
-
Method Details
-
setRemoteUrl
-
setCloudClientId
-
execute
Description copied from class:BaseChenileInterceptor
If this method is over-ridden, make sure that you call doContinue unless you want to end the chain here! This is very important. Also, if there is an exception downstream it needs to be handled here after a call toBaseChenileInterceptor.doContinue(ChenileExchange)
In most cases, you will do fine overridingBaseChenileInterceptor.doPreProcessing(ChenileExchange)
andBaseChenileInterceptor.doPostProcessing(ChenileExchange)
- Specified by:
execute
in interfaceCommand<ChenileExchange>
- Overrides:
execute
in classBaseChenileInterceptor
- Throws:
Exception
-
handleMqtt
In MQTT, if the message comes to the edge, call the service as usual If it is the cloud, handle it like you do HTTP messages- Parameters:
exchange
- the chenile exchange- Throws:
Exception
- in case of any problem downstream
-
handleHttp
- Throws:
Exception
-
handleEdge
In the edge, we will delegate the control to the cloud.- Parameters:
exchange
- the chenile exchange- Throws:
Exception
-
callEdge
private boolean callEdge(ChenileExchange exchange, Object serviceReference, ChainContext.SavePoint savePoint) -
callCloud
Switch to cloud proxy and invoke it via http- Parameters:
exchange
- - the chenile exchange- Returns:
- true if cloud invocation could be done. false if connectivity is a problem
-
handleCloud
In the cloud, execute the actual service Upon successful return, send a message to MQTT topic- Parameters:
exchange
- the Chenile exchange- Throws:
Exception
-
addMqttTargetHeaderIfRequired
If this message came from MQTT then the original source must not be receiving this message since that would create duplicates. Hence, we will create a target header telling the original source to not update itself again.- Parameters:
exchange
- the chenile exchange- Returns:
- the headers map
-
publishMessage
-
toJson
- Throws:
Exception
-
enhanceWarnings
-
switchServiceReference
-
getRemoteProxy
-
bypassInterception
Description copied from class:BaseChenileInterceptor
Over-ride this to bypass interception in special circumstances- Overrides:
bypassInterception
in classBaseChenileInterceptor
- Returns:
-