@ti-engine/core 1.7.1 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +393 -377
- package/LICENSE.md +321 -321
- package/README.md +599 -597
- package/bin/localization/labels.json +122 -122
- package/bin/settings.json +41 -41
- package/bin/start-instance.js +164 -164
- package/components/auditing.js +191 -191
- package/components/connection-observer.js +72 -72
- package/components/definitions.types.js +248 -248
- package/components/exchange/default/default-message-exchange.js +136 -136
- package/components/exchange/default/default-message-receiver.js +101 -101
- package/components/exchange/default/default-message-sender.js +100 -100
- package/components/exchange/message-dispatcher.js +168 -168
- package/components/exchange/message-exchange.js +449 -449
- package/components/exchange/message-handler.js +235 -235
- package/components/exchange/message-memory-cache.js +190 -190
- package/components/exchange/message-observer.js +126 -126
- package/components/exchange/message-receiver.js +181 -181
- package/components/exchange/message-sender.js +143 -143
- package/components/exchange/message-tracer.js +212 -212
- package/components/service-caller.js +370 -370
- package/components/service-consumer.js +131 -131
- package/components/service-executor.js +278 -278
- package/components/service-instance.js +316 -316
- package/components/service-provider.js +251 -251
- package/integrations/redis-integration.js +591 -591
- package/package.json +89 -89
- package/utils/cache.js +772 -772
- package/utils/config.js +103 -103
- package/utils/exceptions.js +368 -368
- package/utils/localization.js +298 -298
- package/utils/logger.js +82 -82
- package/utils/tools.js +632 -632
|
@@ -1,136 +1,136 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* The ti-engine is an open source, free to use—both for personal and commercial projects—framework for the creation of microservice-based solutions using node.js.
|
|
3
|
-
* Copyright © 2021-2023 Boris Kostadinov <kostadinov.boris@gmail.com>
|
|
4
|
-
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
5
|
-
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
6
|
-
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
const MessageExchange = require( "#message-exchange" );
|
|
10
|
-
const config = require( "#config" );
|
|
11
|
-
const exceptions = require( "#exceptions" );
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* The default {@link MessageExchange} behavior for the Ti Engine using Redis for message exchange.
|
|
15
|
-
*
|
|
16
|
-
* @class DefaultMessageExchange
|
|
17
|
-
* @extends MessageExchange
|
|
18
|
-
* @public
|
|
19
|
-
*/
|
|
20
|
-
class DefaultMessageExchange extends MessageExchange {
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* @constructor
|
|
24
|
-
* @param {string} instanceID The unique identifier of the microservice instance using the message exchange.
|
|
25
|
-
* @param {string} serviceDomainName The domain name of the microservice using the message exchange.
|
|
26
|
-
*/
|
|
27
|
-
constructor( instanceID, serviceDomainName ) {
|
|
28
|
-
super( instanceID, serviceDomainName );
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/* Public interface */
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Used to initialize the message exchange.
|
|
35
|
-
* <br/>
|
|
36
|
-
* NOTE: This will create and prepare all necessary message handlers and then enable them simultaneously.
|
|
37
|
-
*
|
|
38
|
-
* @method
|
|
39
|
-
* @param {boolean} configureInbound If set to 'true' it tells the message exchange to set up inbound messaging.
|
|
40
|
-
* @param {boolean} configureOutbound If set to 'true' it tells the message exchange to set up outbound messaging.
|
|
41
|
-
* @returns {Promise}
|
|
42
|
-
* @override
|
|
43
|
-
* @public
|
|
44
|
-
*/
|
|
45
|
-
enableMessaging( configureInbound, configureOutbound ) {
|
|
46
|
-
return new Promise( ( resolve, reject ) => {
|
|
47
|
-
const DefaultMessageSender = require( "#default-message-sender" );
|
|
48
|
-
const DefaultMessageReceiver = require( "#default-message-receiver" );
|
|
49
|
-
|
|
50
|
-
let handlersToEnable = [];
|
|
51
|
-
if ( configureInbound ) {
|
|
52
|
-
let messageResponsesOut = new DefaultMessageSender( MessageExchange.connectionNameResponsesOut );
|
|
53
|
-
let receiveRequestsQueue = config.getSetting( config.setting.MESSAGE_EXCHANGE_QUEUE_PREFIX ) + MessageExchange.pendingQueue + this.serviceDomainName;
|
|
54
|
-
let messageRequestsIn = new DefaultMessageReceiver( MessageExchange.connectionNameRequestsIn, receiveRequestsQueue );
|
|
55
|
-
messageRequestsIn.addMessageObserver( this );
|
|
56
|
-
this.configureInboundMessaging( messageRequestsIn, messageResponsesOut );
|
|
57
|
-
handlersToEnable.push( this.messageResponsesOut.enable() );
|
|
58
|
-
handlersToEnable.push( this.messageRequestsIn.enable() );
|
|
59
|
-
}
|
|
60
|
-
if ( configureOutbound ) {
|
|
61
|
-
let messageRequestsOut = new DefaultMessageSender( MessageExchange.connectionNameRequestsOut );
|
|
62
|
-
let receiveResponsesQueue = config.getSetting( config.setting.MESSAGE_EXCHANGE_QUEUE_PREFIX ) + MessageExchange.processedQueue + this.serviceDomainName + ":" + this.instanceID;
|
|
63
|
-
let messageResponsesIn = new DefaultMessageReceiver( MessageExchange.connectionNameResponsesIn, receiveResponsesQueue );
|
|
64
|
-
messageResponsesIn.addMessageObserver( this );
|
|
65
|
-
this.configureOutboundMessaging( messageRequestsOut, messageResponsesIn );
|
|
66
|
-
handlersToEnable.push( this.messageRequestsOut.enable() );
|
|
67
|
-
handlersToEnable.push( this.messageResponsesIn.enable() );
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
Promise.all( handlersToEnable ).then( () => {
|
|
71
|
-
resolve();
|
|
72
|
-
} ).catch( ( error ) => {
|
|
73
|
-
reject( exceptions.raise( error ) );
|
|
74
|
-
} );
|
|
75
|
-
} );
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Used to gracefully shut down the message exchange.
|
|
80
|
-
*
|
|
81
|
-
* @method
|
|
82
|
-
* @returns {Promise}
|
|
83
|
-
* @override
|
|
84
|
-
* @public
|
|
85
|
-
*/
|
|
86
|
-
disableMessaging() {
|
|
87
|
-
return new Promise( ( resolve, reject ) => {
|
|
88
|
-
let handlersToDisable = [];
|
|
89
|
-
if ( this.configuredInbound ) {
|
|
90
|
-
handlersToDisable.push( this.messageResponsesOut.disable() );
|
|
91
|
-
handlersToDisable.push( this.messageRequestsIn.disable() );
|
|
92
|
-
}
|
|
93
|
-
if ( this.configuredOutbound ) {
|
|
94
|
-
handlersToDisable.push( this.messageRequestsOut.disable() );
|
|
95
|
-
handlersToDisable.push( this.messageResponsesIn.disable() );
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
Promise.all( handlersToDisable ).then( () => {
|
|
99
|
-
resolve();
|
|
100
|
-
} ).catch( ( error ) => {
|
|
101
|
-
reject( exceptions.raise( error ) );
|
|
102
|
-
} );
|
|
103
|
-
} );
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Used to send a message request vie the specified route.
|
|
108
|
-
*
|
|
109
|
-
* @method
|
|
110
|
-
* @param {Message} message The message request to send.
|
|
111
|
-
* @returns {Promise}
|
|
112
|
-
* @override
|
|
113
|
-
* @public
|
|
114
|
-
*/
|
|
115
|
-
sendMessageRequest( message ) {
|
|
116
|
-
let sendQueue = config.getSetting( config.setting.MESSAGE_EXCHANGE_QUEUE_PREFIX ) + MessageExchange.pendingQueue + message.destination.route;
|
|
117
|
-
return this.messageRequestsOut.send( message, sendQueue );
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Used to send a message response via the specified route.
|
|
122
|
-
*
|
|
123
|
-
* @method
|
|
124
|
-
* @param {Message} message The message response to send.
|
|
125
|
-
* @returns {Promise}
|
|
126
|
-
* @override
|
|
127
|
-
* @public
|
|
128
|
-
*/
|
|
129
|
-
sendMessageResponse( message ) {
|
|
130
|
-
let sendQueue = config.getSetting( config.setting.MESSAGE_EXCHANGE_QUEUE_PREFIX ) + MessageExchange.processedQueue + message.source.route + ":" + message.source.instanceID;
|
|
131
|
-
return this.messageResponsesOut.send( message, sendQueue );
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
module.exports = DefaultMessageExchange;
|
|
1
|
+
/*
|
|
2
|
+
* The ti-engine is an open source, free to use—both for personal and commercial projects—framework for the creation of microservice-based solutions using node.js.
|
|
3
|
+
* Copyright © 2021-2023 Boris Kostadinov <kostadinov.boris@gmail.com>
|
|
4
|
+
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
5
|
+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
6
|
+
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const MessageExchange = require( "#message-exchange" );
|
|
10
|
+
const config = require( "#config" );
|
|
11
|
+
const exceptions = require( "#exceptions" );
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The default {@link MessageExchange} behavior for the Ti Engine using Redis for message exchange.
|
|
15
|
+
*
|
|
16
|
+
* @class DefaultMessageExchange
|
|
17
|
+
* @extends MessageExchange
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
class DefaultMessageExchange extends MessageExchange {
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @constructor
|
|
24
|
+
* @param {string} instanceID The unique identifier of the microservice instance using the message exchange.
|
|
25
|
+
* @param {string} serviceDomainName The domain name of the microservice using the message exchange.
|
|
26
|
+
*/
|
|
27
|
+
constructor( instanceID, serviceDomainName ) {
|
|
28
|
+
super( instanceID, serviceDomainName );
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/* Public interface */
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Used to initialize the message exchange.
|
|
35
|
+
* <br/>
|
|
36
|
+
* NOTE: This will create and prepare all necessary message handlers and then enable them simultaneously.
|
|
37
|
+
*
|
|
38
|
+
* @method
|
|
39
|
+
* @param {boolean} configureInbound If set to 'true' it tells the message exchange to set up inbound messaging.
|
|
40
|
+
* @param {boolean} configureOutbound If set to 'true' it tells the message exchange to set up outbound messaging.
|
|
41
|
+
* @returns {Promise}
|
|
42
|
+
* @override
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
enableMessaging( configureInbound, configureOutbound ) {
|
|
46
|
+
return new Promise( ( resolve, reject ) => {
|
|
47
|
+
const DefaultMessageSender = require( "#default-message-sender" );
|
|
48
|
+
const DefaultMessageReceiver = require( "#default-message-receiver" );
|
|
49
|
+
|
|
50
|
+
let handlersToEnable = [];
|
|
51
|
+
if ( configureInbound ) {
|
|
52
|
+
let messageResponsesOut = new DefaultMessageSender( MessageExchange.connectionNameResponsesOut );
|
|
53
|
+
let receiveRequestsQueue = config.getSetting( config.setting.MESSAGE_EXCHANGE_QUEUE_PREFIX ) + MessageExchange.pendingQueue + this.serviceDomainName;
|
|
54
|
+
let messageRequestsIn = new DefaultMessageReceiver( MessageExchange.connectionNameRequestsIn, receiveRequestsQueue );
|
|
55
|
+
messageRequestsIn.addMessageObserver( this );
|
|
56
|
+
this.configureInboundMessaging( messageRequestsIn, messageResponsesOut );
|
|
57
|
+
handlersToEnable.push( this.messageResponsesOut.enable() );
|
|
58
|
+
handlersToEnable.push( this.messageRequestsIn.enable() );
|
|
59
|
+
}
|
|
60
|
+
if ( configureOutbound ) {
|
|
61
|
+
let messageRequestsOut = new DefaultMessageSender( MessageExchange.connectionNameRequestsOut );
|
|
62
|
+
let receiveResponsesQueue = config.getSetting( config.setting.MESSAGE_EXCHANGE_QUEUE_PREFIX ) + MessageExchange.processedQueue + this.serviceDomainName + ":" + this.instanceID;
|
|
63
|
+
let messageResponsesIn = new DefaultMessageReceiver( MessageExchange.connectionNameResponsesIn, receiveResponsesQueue );
|
|
64
|
+
messageResponsesIn.addMessageObserver( this );
|
|
65
|
+
this.configureOutboundMessaging( messageRequestsOut, messageResponsesIn );
|
|
66
|
+
handlersToEnable.push( this.messageRequestsOut.enable() );
|
|
67
|
+
handlersToEnable.push( this.messageResponsesIn.enable() );
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
Promise.all( handlersToEnable ).then( () => {
|
|
71
|
+
resolve();
|
|
72
|
+
} ).catch( ( error ) => {
|
|
73
|
+
reject( exceptions.raise( error ) );
|
|
74
|
+
} );
|
|
75
|
+
} );
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Used to gracefully shut down the message exchange.
|
|
80
|
+
*
|
|
81
|
+
* @method
|
|
82
|
+
* @returns {Promise}
|
|
83
|
+
* @override
|
|
84
|
+
* @public
|
|
85
|
+
*/
|
|
86
|
+
disableMessaging() {
|
|
87
|
+
return new Promise( ( resolve, reject ) => {
|
|
88
|
+
let handlersToDisable = [];
|
|
89
|
+
if ( this.configuredInbound ) {
|
|
90
|
+
handlersToDisable.push( this.messageResponsesOut.disable() );
|
|
91
|
+
handlersToDisable.push( this.messageRequestsIn.disable() );
|
|
92
|
+
}
|
|
93
|
+
if ( this.configuredOutbound ) {
|
|
94
|
+
handlersToDisable.push( this.messageRequestsOut.disable() );
|
|
95
|
+
handlersToDisable.push( this.messageResponsesIn.disable() );
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
Promise.all( handlersToDisable ).then( () => {
|
|
99
|
+
resolve();
|
|
100
|
+
} ).catch( ( error ) => {
|
|
101
|
+
reject( exceptions.raise( error ) );
|
|
102
|
+
} );
|
|
103
|
+
} );
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Used to send a message request vie the specified route.
|
|
108
|
+
*
|
|
109
|
+
* @method
|
|
110
|
+
* @param {Message} message The message request to send.
|
|
111
|
+
* @returns {Promise}
|
|
112
|
+
* @override
|
|
113
|
+
* @public
|
|
114
|
+
*/
|
|
115
|
+
sendMessageRequest( message ) {
|
|
116
|
+
let sendQueue = config.getSetting( config.setting.MESSAGE_EXCHANGE_QUEUE_PREFIX ) + MessageExchange.pendingQueue + message.destination.route;
|
|
117
|
+
return this.messageRequestsOut.send( message, sendQueue );
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Used to send a message response via the specified route.
|
|
122
|
+
*
|
|
123
|
+
* @method
|
|
124
|
+
* @param {Message} message The message response to send.
|
|
125
|
+
* @returns {Promise}
|
|
126
|
+
* @override
|
|
127
|
+
* @public
|
|
128
|
+
*/
|
|
129
|
+
sendMessageResponse( message ) {
|
|
130
|
+
let sendQueue = config.getSetting( config.setting.MESSAGE_EXCHANGE_QUEUE_PREFIX ) + MessageExchange.processedQueue + message.source.route + ":" + message.source.instanceID;
|
|
131
|
+
return this.messageResponsesOut.send( message, sendQueue );
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
module.exports = DefaultMessageExchange;
|
|
@@ -1,102 +1,102 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* The ti-engine is an open source, free to use—both for personal and commercial projects—framework for the creation of microservice-based solutions using node.js.
|
|
3
|
-
* Copyright © 2021-2025 Boris Kostadinov <kostadinov.boris@gmail.com>
|
|
4
|
-
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
5
|
-
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
6
|
-
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
const MessageReceiver = require( "#message-receiver" );
|
|
10
|
-
const memoryCache = require( "#message-memory-cache" );
|
|
11
|
-
const config = require( "#config" );
|
|
12
|
-
const exceptions = require( "#exceptions" );
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* The default {@link MessageReceiver} behavior for the Ti Engine using Redis for message exchange.
|
|
16
|
-
*
|
|
17
|
-
* @class DefaultMessageReceiver
|
|
18
|
-
* @extends MessageReceiver
|
|
19
|
-
* @public
|
|
20
|
-
*/
|
|
21
|
-
class DefaultMessageReceiver extends MessageReceiver {
|
|
22
|
-
|
|
23
|
-
#memoryCache;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* @constructor
|
|
27
|
-
* @param {string} identifier An identifier for this message handler. Should be unique in the context of the message exchange.
|
|
28
|
-
* @param {string} receiveQueue The queue from which the messages will be received.
|
|
29
|
-
*/
|
|
30
|
-
constructor( identifier, receiveQueue ) {
|
|
31
|
-
super( identifier, receiveQueue );
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Used to initialize and enable the communication capabilities of the handler.
|
|
36
|
-
*
|
|
37
|
-
* @method
|
|
38
|
-
* @returns {Promise}
|
|
39
|
-
* @override
|
|
40
|
-
* @public
|
|
41
|
-
*/
|
|
42
|
-
enable() {
|
|
43
|
-
return new Promise( ( resolve, reject ) => {
|
|
44
|
-
this.#memoryCache = memoryCache.create( this.connectionIdentifier );
|
|
45
|
-
this.#memoryCache.addConnectionObserver( this );
|
|
46
|
-
this.#memoryCache.initialize().then( () => {
|
|
47
|
-
this.isReceiving = true;
|
|
48
|
-
this.receive();
|
|
49
|
-
resolve();
|
|
50
|
-
} ).catch( ( error ) => {
|
|
51
|
-
reject( exceptions.raise( error ) );
|
|
52
|
-
} );
|
|
53
|
-
} );
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Used to shut down and disable the communication behavior of the handler.
|
|
58
|
-
*
|
|
59
|
-
* @method
|
|
60
|
-
* @returns {Promise}
|
|
61
|
-
* @override
|
|
62
|
-
* @public
|
|
63
|
-
*/
|
|
64
|
-
disable() {
|
|
65
|
-
return new Promise( ( resolve, reject ) => {
|
|
66
|
-
this.isAvailable = false;
|
|
67
|
-
this.#memoryCache.shutDown().then( () => {
|
|
68
|
-
this.isReceiving = false;
|
|
69
|
-
this.#memoryCache = null;
|
|
70
|
-
resolve();
|
|
71
|
-
} ).catch( ( error ) => {
|
|
72
|
-
reject( exceptions.raise( error ) );
|
|
73
|
-
} );
|
|
74
|
-
} );
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Used to receive messages.
|
|
79
|
-
* <br/>
|
|
80
|
-
* NOTE: The default message exchange works with lightweight messages (i.e. will keep the payloads stored in Redis while exchanging).
|
|
81
|
-
*
|
|
82
|
-
* @method
|
|
83
|
-
* @returns {Promise<Message>}
|
|
84
|
-
* @override
|
|
85
|
-
* @public
|
|
86
|
-
*/
|
|
87
|
-
onReceive() {
|
|
88
|
-
return new Promise( ( resolve, reject ) => {
|
|
89
|
-
// NOTE: The method execution will block on this call until a message is received:
|
|
90
|
-
this.#memoryCache.receiveMessage( this.receiveQueue ).then( ( lightweightMessage ) => {
|
|
91
|
-
return this.#memoryCache.retrieveMessagePayload( lightweightMessage, config.getSetting( config.setting.MESSAGE_EXCHANGE_MESSAGE_STORE ) );
|
|
92
|
-
} ).then( ( message ) => {
|
|
93
|
-
resolve( message );
|
|
94
|
-
} ).catch( ( error ) => {
|
|
95
|
-
reject( exceptions.raise( error ) );
|
|
96
|
-
} );
|
|
97
|
-
} );
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
|
|
1
|
+
/*
|
|
2
|
+
* The ti-engine is an open source, free to use—both for personal and commercial projects—framework for the creation of microservice-based solutions using node.js.
|
|
3
|
+
* Copyright © 2021-2025 Boris Kostadinov <kostadinov.boris@gmail.com>
|
|
4
|
+
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
5
|
+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
6
|
+
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const MessageReceiver = require( "#message-receiver" );
|
|
10
|
+
const memoryCache = require( "#message-memory-cache" );
|
|
11
|
+
const config = require( "#config" );
|
|
12
|
+
const exceptions = require( "#exceptions" );
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The default {@link MessageReceiver} behavior for the Ti Engine using Redis for message exchange.
|
|
16
|
+
*
|
|
17
|
+
* @class DefaultMessageReceiver
|
|
18
|
+
* @extends MessageReceiver
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
class DefaultMessageReceiver extends MessageReceiver {
|
|
22
|
+
|
|
23
|
+
#memoryCache;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @constructor
|
|
27
|
+
* @param {string} identifier An identifier for this message handler. Should be unique in the context of the message exchange.
|
|
28
|
+
* @param {string} receiveQueue The queue from which the messages will be received.
|
|
29
|
+
*/
|
|
30
|
+
constructor( identifier, receiveQueue ) {
|
|
31
|
+
super( identifier, receiveQueue );
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Used to initialize and enable the communication capabilities of the handler.
|
|
36
|
+
*
|
|
37
|
+
* @method
|
|
38
|
+
* @returns {Promise}
|
|
39
|
+
* @override
|
|
40
|
+
* @public
|
|
41
|
+
*/
|
|
42
|
+
enable() {
|
|
43
|
+
return new Promise( ( resolve, reject ) => {
|
|
44
|
+
this.#memoryCache = memoryCache.create( this.connectionIdentifier );
|
|
45
|
+
this.#memoryCache.addConnectionObserver( this );
|
|
46
|
+
this.#memoryCache.initialize().then( () => {
|
|
47
|
+
this.isReceiving = true;
|
|
48
|
+
this.receive();
|
|
49
|
+
resolve();
|
|
50
|
+
} ).catch( ( error ) => {
|
|
51
|
+
reject( exceptions.raise( error ) );
|
|
52
|
+
} );
|
|
53
|
+
} );
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Used to shut down and disable the communication behavior of the handler.
|
|
58
|
+
*
|
|
59
|
+
* @method
|
|
60
|
+
* @returns {Promise}
|
|
61
|
+
* @override
|
|
62
|
+
* @public
|
|
63
|
+
*/
|
|
64
|
+
disable() {
|
|
65
|
+
return new Promise( ( resolve, reject ) => {
|
|
66
|
+
this.isAvailable = false;
|
|
67
|
+
this.#memoryCache.shutDown().then( () => {
|
|
68
|
+
this.isReceiving = false;
|
|
69
|
+
this.#memoryCache = null;
|
|
70
|
+
resolve();
|
|
71
|
+
} ).catch( ( error ) => {
|
|
72
|
+
reject( exceptions.raise( error ) );
|
|
73
|
+
} );
|
|
74
|
+
} );
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Used to receive messages.
|
|
79
|
+
* <br/>
|
|
80
|
+
* NOTE: The default message exchange works with lightweight messages (i.e. will keep the payloads stored in Redis while exchanging).
|
|
81
|
+
*
|
|
82
|
+
* @method
|
|
83
|
+
* @returns {Promise<Message>}
|
|
84
|
+
* @override
|
|
85
|
+
* @public
|
|
86
|
+
*/
|
|
87
|
+
onReceive() {
|
|
88
|
+
return new Promise( ( resolve, reject ) => {
|
|
89
|
+
// NOTE: The method execution will block on this call until a message is received:
|
|
90
|
+
this.#memoryCache.receiveMessage( this.receiveQueue ).then( ( lightweightMessage ) => {
|
|
91
|
+
return this.#memoryCache.retrieveMessagePayload( lightweightMessage, config.getSetting( config.setting.MESSAGE_EXCHANGE_MESSAGE_STORE ) );
|
|
92
|
+
} ).then( ( message ) => {
|
|
93
|
+
resolve( message );
|
|
94
|
+
} ).catch( ( error ) => {
|
|
95
|
+
reject( exceptions.raise( error ) );
|
|
96
|
+
} );
|
|
97
|
+
} );
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
}
|
|
101
|
+
|
|
102
102
|
module.exports = DefaultMessageReceiver;
|