1。はじめに
エンタープライズシステムの開発により、アプリケーションは主に分散構造を採用しており、ネットワークの安定性に大きく依存しています。ただし、ネットワークの固有の不安定性により、システム開発中のネットワーク不安定性の場合にアプリケーションの堅牢性を確保する方法を検討する必要があります。ネットワークタイムアウトの設定は、アプリケーションの堅牢性を確保する手段の1つです。ネットワークタイムアウト設定の設定後、リクエストは設定時間の完了に失敗した場合に終了することを余儀なくされ、プログラムに無制限のスレッドブロックがないことを確認し、アプリケーションの可用性を効果的に改善します。
以下ではあまり言いません。詳細な紹介を一緒に見てみましょう。
2。タイムアウトが設定されていないこととタイムアウト設定の比較
1。ネットワークリクエストの凡例:
ネットワークリクエストタイムアウトケース
2。タイムアウト時間を設定した後、凡例をリクエストします。
ネットワークリクエストタイムアウトケース - タイムアウトを設定します
3.一般的なネットワークタイムアウト設定
1。HTTPCLIENTタイムアウト設定(Spring Bean)
構成
<bean id = "multithreadedhttpconnectionmanager"> <プロパティ名= "params"> <bean> <プロパティ名= "maxtotalconnections" value = "$ {maxtotalconnections:300}" /> <プロパティ名= "defaultmaxconnectionsperhost" value "アウト、ミリ秒。 - > <プロパティname = "ConnectionTimeout" Value = "$ {connectTimeout:10000}" /> <! - ソケットタイムアウト、Milliseconds。 - > <プロパティ名= "sotimeout" value = "$ {readtimeout:600000}"/> <property name = "stalecheckingenabled" value = "$ {stalecheckingenabled:true}"/> </bean> </property> </bean> <bean id = "httpclient" bean = "multithreadedhttpconnectionmanager"/> </constructor-arg> </bean>httpinvokerの使用シナリオ
httpinvokerrequestexecutorを構成し、httpinvokerproxyfactorybeanでデフォルトで使用されるsimplehttpinvokerrequestexecutorをオーバーライドし、ネットワークタイムアウトを構成します。 「構成」を参照してください。
<bean id = "httpinvokerrequestexecutor"> <constructor-arg> <ref bean = "httpclient" /> < /constructor-arg> < /bean> <bean id = "xxxxservice" value = "com.xxxxservice" /> <property name = "httpinvokerrequestexecutor" ref = "httpinvokerrequestexecutor" /> < /bean>
2。HTTPCLIENTタイムアウト設定(ハードコード化)
サンプル
requestConfig config = requestConfig.custom().Setsockettimeout(1*1000)//ソケットソケットタイムアウト、ミリ秒。 .setConnectionRequestTimeout(1*1000)//接続プールを使用して接続を管理する場合、接続プールからミリ秒の接続タイムアウト時間を取得します。 .setConnectTimeout(5*1000)//接続確立タイムアウト、ミリ秒。 。建てる(); closeablehttpclient httpclient = httpclient.custom().setdefaultrequestconfig(config)// .build(); closeablehttpresponse httpresponse = httpclient.execute(httpget); //リクエストを実行します
3.タイムアウト設定をメールで送信します
Springフレームワークに基づいて開発されたプロジェクトは非常に簡単に使用できます
org.springframework.mail.javamail.javamailsenderimplは、電子メールのリマインダーやその他の機能を実装しています。
構成
<bean id = "mailsender" p:host = "$ {mailsender.host}" p:username = "$ {mailsender.username}" p:password = "$ {mailsender.password}"> <プロパティ名= "javamailproperties"> <props> <props key = "mail.smtp.auth"> $ {mailsender.smtp.auth:true} </prop> <prop key = "mail.smtp.timeout"> $ {mailsender.smtp.timeout:10000} </prop> <prop key = "mail.smtp.ConnectionTimeOut"> $ $ $ $ $ {emaysend.smtp.cnection </prop> </props> </property> </bean>JavamailPropertiesの説明
注:プロパティパラメーター名リストは、Javamail APIドキュメントを照会できます。
要約します
上記は、この記事のコンテンツ全体です。この記事の内容には、すべての人の研究や仕事に特定の参照値があることを願っています。ご質問がある場合は、メッセージを残してコミュニケーションをとることができます。 wulin.comへのご支援ありがとうございます。
参照してください