Background technology
Nowadays, more and more enterprises, unit are faced with various data integrations and system combination, RPC middleware Technology such as CORBA, DCOM, RMI are also arisen at the historic moment, but owing to adopt RPC Synchronous Processing technology, all exist many shortcomings on performance, robustness, extensibility.And message based asynchronous process model adopts the recall feature of unblock, and the sender sends to message server with message, and message server is given the recipient with forwards again in suitable; It is asynchronous sending and receiving, the sender need not to wait for, the life cycle of the two also can be identical, and the sender can pass to a plurality of recipients indirectly with message, improved performance, extensibility and the robustness of program greatly, this makes the asynchronous process model have more attraction compared with the Synchronous Processing model on Distributed Application.
Publish/subscribe (publish/subscribe wherein, abbreviation pub/sub) system is a kind of messaging system based on the Distributed Calculation platform, also can claim middleware system, be used to the communication mode that a kind of loose coupling is provided between each participant in the distributed system.In the pub/sub system, the producer (publisher) of message and the consumer (subscriber) of message are arranged, the publisher issues message to the interested subscriber of this message by the middle-agent, and the subscriber differ establish a capital online, will get up message stores this moment, when reaching the standard grade, again message is sent etc. next subscriber.The message persistence just provides the function of storing message, and it has guaranteed the reliability of system.Make that the persistence message that sends to server end can both correctly be handled no matter be that server is worked as machine or client connection failure.Yet, because data file may be very big, that how with the least possible time carry out data storage, read, operation such as deletion becomes most important.These performances are relevant with adopting which type of data storage method, and what propose here is the mode of file storage, it by the persistence message stores in file, and reach storage, the deletion of message, the high-performance performance of extraction.
At present message stores mechanism mainly contains database storage, just by the persistence message stores in specific database, but because to create the expense that database connects very big, thereby influenced the storage of message, the performance of deletion etc.
Summary of the invention
The present invention will solve the defective that the message persistence exists in the existing pub/sub system, a kind of file memory method based on publish/subscribe system of high performance storage, deletion and the extraction to message is provided, and is the method that adopts when in a kind of distribution subscription system message being carried out persistence.
The technical solution adopted for the present invention to solve the technical problems: this file memory method based on distribution subscription system, in the pub/sub system, for all creating a message container table in each message destination, and the message container table of each destination all will exist in the list structure.Simultaneously the message container table of each destination is exactly the interface of buffered message, and methods such as the storage that can provide by the message container table, deletion, modification come message is carried out corresponding action.This makes operation file easily convenient as the table in internal memory of operation.Being achieved as follows of the concrete grammar of message container table: what deposit in the message container table is the message summation on specific purpose ground, and each message is so that (messageId, message) (keyword, the value) of form is to storing.When storing message is right, for each message is set up an index, index record the keyword of each message and two data places of value file and in the original position of file.The structure size of each index is all the same, and all index all leave in the index file with the form of chained list.And the index relative of each message and its correspondence is so that (messageId, IndexItem) (keyword, the value) of form is to leaving in the message index table.Concrete message is stored hereof with the form of data file, the big I manual configuration of file, but under the situation that file size configures, the message size of depositing hereof can not be above the size of each file that configures.Concrete steps are as follows:
After system start-up is good, automatically set up a message container table for each destination, and each destination has only a message container table, all message in this destination all are kept in this message container table with the right form of keyword/value, wherein keyword is corresponding to the messageId (messageId defines in the bag that sends over of message) of a message in the destination, and value is then to content that should message object.
1. the message when a sustainable subscription arrives server end:
From message, obtain the destination title that it will mail to,, find corresponding message container table,, then create a new message container table if there is not such message container table according to this destination title.Then, find corresponding messageId and its value from the message that arrives, the method for the storing message that provides by the message container table is saved in message in the file.
2. if will from file, take out a persistence message, then:
The same message container table that finds this message correspondence, and the method for the cancellation that provides according to message container table breath is removed to read in the file a piece of news and is got final product.
3. if will from file, delete a persistence message, then:
The same message container table that finds this message correspondence, the method according to the deletion message of message container table finds this message hereof, and deletion gets final product.
The effect that the present invention is useful is: realized a message container table with file memory function, can operate this message container table as the common list structure of operation simultaneously, promptly by put (), get (), remove methods such as () is carried out, and does not need to know the details of bottom when carrying out the persistence message stores.The while another advantage of the present invention is that its storage speed is fast more a lot of than database, because it does not need to carry out the connection of database.
Embodiment
The invention will be described further below in conjunction with drawings and Examples:
For making purpose of the present invention, technical scheme and advantage are clearer, and below the present invention being used for java is the pub/sub system of development language, and will be described in further detail in conjunction with the accompanying drawings.
Under the java language environment, the present invention has been equivalent to realize a Map with file memory function, and the data of this Map storage can still can not used the data in the Map along with the closing or collapse and disappear of server when next server restarts.Simultaneously, realize the total interface of Map, conveniently call for other class.The corresponding MapContainer (message container table) in each destination, the MessageId of the message in the middle of each destination is unique, so leave in the middle of the MapContainer as key with MessageId.Introduce some cardinal principles earlier:
1) class MapContainer has realized that (Map provides a general element storage means to the map interface.The Map collection class is used for storage element to (being called " key " and " value "), and wherein each key is mapped to a value), and with the difference of commonly used map be: there is the internal memory the inside to object in map commonly used, and the present invention deposits object on the disk.Using method is the same with common map, and (key object) (is associated designated value, and deposits the map the inside)/MapContainer.get (key) (returning the value related with assignment key) MapContainer.put with assignment key;
2) the corresponding MapContainer in each destination creates a MapContainer for each destination, stores the message of this destination.All MapContainer are put among the map.Process is: receive the corresponding MapContainer-in the destination of message-this message of acquisition-find〉and MapContainer.put (messageId, message);
3) because size of message may be very big, so that data file may be very is big, the position of localization message data in the middle of this document correctly read message data then fast.Therefore the present invention adopts index, is index of each message establishing owing to be with the Map access, so index record key and two data of value original positions at file.All index link with a list, and the size of each index all is the same.Also adopted file read-write, because the access of message need be in the middle of file reading and writing at random, RandomAccessFile is provided in JDK this class, support the random read-write of file, but such I/O operating efficiency is lower, and byte of every read/write is carried out an I/O operation with regard to needing to disk.Add that by the RandomAccessFile class buffering read-write mechanism improves the speed of reading and writing data.Each data all has a data head simultaneously.Data head comprises a constant and data length, and it is correct having only the constant that ought read during read data, thinks that just these data are correct.
Introduce some structures and the realization of message stores below:
Employing index mechanism, the structure of IndexItem:
short?MAGIC |
Be used to judge whether this index is correct |
long?offset |
The position of this index in index |
long?previousItem |
The position of previous index |
long?nextItem |
The position of the index in back |
long?keyOffset |
The position of key |
int?keyFile |
The file at key place |
long?valueOffset |
The position of value |
int?valueFile |
The file at value place |
boolean?active |
Whether this index is movable |
The structure of DataItem (being stored in the data format on the file):
?short?MAGIC |
Be used to judge whether this index is correct |
?long?offset |
The position of this index in index |
?int?file |
The file at place |
?int?size |
The length of data |
Introduce the data storage procedure based on file storage of the present invention (based on figure one) below:
The corresponding MapContainer in each destination sets up corresponding MapContainer according to messageId in the destination and message value, and all MapContainer leave among the map.
● deposit data: call MapContainer.put (messageId, message)
1. create the IndexItem (index entry) of a sky
2. find idle file area by findSpaceForData () function
3. be keyData that messageId writes file, and create a dataItem, at dataItem,
Deposit the file at messageId place and in the side-play amount of this document
4. the value of IndexItem is set, i.e. keyFile and keyOffset according to dataItem
5. find idle file area by findSpaceForData () function
6. be valueData that message message body writes file, and create a dataItem, deposit the file at message place and in the side-play amount of this document
7. the value of IndexItem is set, i.e. valueFile and valueOffset according to dataItem
8. IndexItem is put into a list, and deposit the index file in
9. indexItem, keyDate are put into two Map, one with indexItem as key, another with keyData as key
● fetch data: call MapContainer.get (messageId)
1. (deposit that (messageId IndexItem) obtains IndexItem (index entry) according to keyData in (key, value) the right list structure) of form from the message index table
2. in specific I ndexItem, obtain leaving in the file at message place wherein and, also promptly obtain DataItem in the side-play amount of file
3. obtain existing concrete message data on the file by DataManager according to DataItem
● deleted data: call MapContainer.remove (messageId)
1. (deposit that (messageId IndexItem) obtains IndexItem (index entry) according to keyData in (key, value) the right list structure) of form from the message index table
From two message index tables (deposit (and messageId, IndexItem) form (key, value) to deposit that (IndexItem messageId) deletes corresponding indexItem, keyData item in (key, value) the right list structure) of form
3. obtain leaving in the file at message place wherein and, also just obtained DataItem (as before) according to IndexItem in the side-play amount of file
4. delete the concrete message data that exists on the file according to DataItem by DataManager
5. from the list chained list, delete the IndexItem item
6. IndexItem is reset active=false
7.updata this IndexItem is in the index file
The foregoing description is used for the present invention that explains, rather than limits the invention, and in the protection range of spirit of the present invention and claim, any modification and change to the present invention makes all fall into protection scope of the present invention.