Print"IT168 Knowledge Base"
Goal: Enter a keyword to retrieve eligible resource entries from a different repository. Where the repository has data on the local hard disk, data on the remote Web, where the previous resource search is done by the application Localsearcher, and the search for the latter resource is done by Remwebsearcher, and the search portal is a Web site Dsearchweb. Dsearchweb received the search request, respectively, two threads to call Localsearcher and Remwebsearcher, and so their two results are returned after the results are grouped together to show the customer, which because of remwebsearcher work pressure is relatively large , he and Dsearchweb are not on a machine, they communicate with each other by rmoting. In practical applications, Remwebsearcher can have multiple sets of pressure to balance the search, and if a server searches for a timeout or throws an exception, the main entry program cannot crash. And we're talking about the results of distributed search after the hands of the results need to do some sorting or sensitive word filtering operations.
The local search and remote search are only a few sample code, should have used WebRequest and Lucene to improve the code, and later feel and this topic from some far away from the first.
First, define the relevant interfaces and public classes.
<summary>
Search result class, to be marked as serializable in order to be transmitted using remoting
</summary>
[Serializable]
public class SearchResult
{
Public SearchResult (String rst)
{
result = rst;
}
string result;
public string Result
{
get {return result;}
set {result = value;}
}
}
<summary>
Searcher interface
</summary>
public interface Isearcher
{
List < SearchResult > Search (string keyword);
}
then prepare a local search engine and a remote search engine.
<summary>
Local Search Device
</summary>
public class Localsearcher:isearcher
{
#region Isearcher Members
Public List < SearchResult > Search (string keyword)
{
Simulate using Lucene to search for local indexes
List < SearchResult > list = new List < SearchResult > ();
List. ADD (New SearchResult ("Autumn does not Come"));
Thread.Sleep (1000);
List. ADD (New SearchResult ("The Wind smoke Rolling Sing hero"));
Thread.Sleep (1000);
List. ADD (New SearchResult ("Red lotus root Incense residue Yuchu");
return list;
}
#endregion
}
The remote search server is a console program, and the following is its main class
<summary>
Remote Search class, in order to expose remoting, inherit MarshalByRefObject
</summary>
public class Remwebsearcher:marshalbyrefobject,isearcher
{
#region Isearcher Members
Public List < SearchResult > Search (string keyword)
{
Simulate using WebRequest to crawl a Web page and find a qualifying result entry
List < SearchResult > list = new List < SearchResult > ();
List. ADD ("I am a small qinglong, small Qinglong")); SearchResult
Thread.Sleep (1000);
List. ADD (New SearchResult (") difficult to meet, the east wind can not afford to waste." " ));
Thread.Sleep (1000);
List. ADD (The new SearchResult ("Why the name of love, to the end of the return.") " ));
return list;
}
#endregion
}
<summary>
To start our search server, the real environment is more complex than this
</summary>
Class Program
{
static void Main (string [] args)
{
TcpChannel channel = new TcpChannel (8080);
ChannelServices.RegisterChannel (channel, false);
RemotingConfiguration.RegisterWellKnownServiceType (
typeof (Remweb.remwebsearcher),
"Remwebsearcher", Wellknownobjectmode.singleton);
Console.WriteLine ("Service started, carriage return stopped");
Console.read ();
Channel. Stoplistening (NULL);
Channelservices.unregisterchannel (channel);
Console.WriteLine ("Service stopped, return after carriage");
Here are two read (), one is not halfway.
Console.read ();
Console.read ();
}
}
Next, we make a website for users to use, we made Ajax
< form ID = "Form1" runat = "server" >
< Asp:scriptmanager ID = "ScriptManager1" runat = "server"/>
< div >
< Asp:textbox ID = "Txtkeyword" runat = "server" ></asp:textbox >
< Asp:button ID = "btnsearch" runat = "server" Text = "search" OnClick = "btnSearch_Click"/>
< asp:updateprogress ID = "UpdateProgress1" runat = "server" Associatedupdatepanelid = "UpdatePanel1" >
< ProgressTemplate >
Please wait
</progresstemplate >
</asp:updateprogress >
< br/>
< Asp:updatepanel ID = "UpdatePanel1" runat = "server" UpdateMode = "Conditional" >
< ContentTemplate >
< Asp:repeater ID = "rptresults" runat = "server" >
< ItemTemplate >
< Asp:label runat = "server" ID = "Label1" Text = ' <%# Eval (' result ')% > '/> < br/>
</ItemTemplate >
</asp:repeater >