📄 readthrottlefilter.html
字号:
<a name="237" href="#237">237</a> <strong class="jxr_keyword">this</strong>.maxSessionBufferSize = maxSessionBufferSize;<a name="238" href="#238">238</a> }<a name="239" href="#239">239</a> <a name="240" href="#240">240</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> setMaxServiceBufferSize(<strong class="jxr_keyword">int</strong> maxServiceBufferSize) {<a name="241" href="#241">241</a> <strong class="jxr_keyword">if</strong> (maxServiceBufferSize < 0) {<a name="242" href="#242">242</a> maxServiceBufferSize = 0;<a name="243" href="#243">243</a> }<a name="244" href="#244">244</a> <strong class="jxr_keyword">this</strong>.maxServiceBufferSize = maxServiceBufferSize;<a name="245" href="#245">245</a> }<a name="246" href="#246">246</a> <a name="247" href="#247">247</a> <em class="jxr_javadoccomment">/**</em><a name="248" href="#248">248</a> <em class="jxr_javadoccomment"> * Sets the maximum amount of data in the buffer of the {@link ExecutorFilter}</em><a name="249" href="#249">249</a> <em class="jxr_javadoccomment"> * for all {@link IoSession} whose {@link IoFilterChain} has been configured by</em><a name="250" href="#250">250</a> <em class="jxr_javadoccomment"> * this builder. Specify {@code 0} or a smaller value to disable.</em><a name="251" href="#251">251</a> <em class="jxr_javadoccomment"> */</em><a name="252" href="#252">252</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> setMaxGlobalBufferSize(<strong class="jxr_keyword">int</strong> maxGlobalBufferSize) {<a name="253" href="#253">253</a> <strong class="jxr_keyword">if</strong> (maxGlobalBufferSize < 0) {<a name="254" href="#254">254</a> maxGlobalBufferSize = 0;<a name="255" href="#255">255</a> }<a name="256" href="#256">256</a> <strong class="jxr_keyword">this</strong>.maxGlobalBufferSize = maxGlobalBufferSize;<a name="257" href="#257">257</a> }<a name="258" href="#258">258</a> <a name="259" href="#259">259</a> <em class="jxr_javadoccomment">/**</em><a name="260" href="#260">260</a> <em class="jxr_javadoccomment"> * Returns the size estimator currently in use.</em><a name="261" href="#261">261</a> <em class="jxr_javadoccomment"> */</em><a name="262" href="#262">262</a> <strong class="jxr_keyword">public</strong> <a href="../../../../../org/apache/mina/filter/traffic/MessageSizeEstimator.html">MessageSizeEstimator</a> getMessageSizeEstimator() {<a name="263" href="#263">263</a> <strong class="jxr_keyword">return</strong> messageSizeEstimator;<a name="264" href="#264">264</a> }<a name="265" href="#265">265</a> <a name="266" href="#266">266</a> <em class="jxr_javadoccomment">/**</em><a name="267" href="#267">267</a> <em class="jxr_javadoccomment"> * Returns the current amount of data in the buffer of the {@link ExecutorFilter}</em><a name="268" href="#268">268</a> <em class="jxr_javadoccomment"> * for the specified {@link IoSession}.</em><a name="269" href="#269">269</a> <em class="jxr_javadoccomment"> */</em><a name="270" href="#270">270</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">int</strong> getSessionBufferSize(<a href="../../../../../org/apache/mina/common/IoSession.html">IoSession</a> session) {<a name="271" href="#271">271</a> <a href="../../../../../org/apache/mina/filter/traffic/ReadThrottleFilter.html">State</a> state = (State) session.getAttribute(STATE);<a name="272" href="#272">272</a> <strong class="jxr_keyword">if</strong> (state == <strong class="jxr_keyword">null</strong>) {<a name="273" href="#273">273</a> <strong class="jxr_keyword">return</strong> 0;<a name="274" href="#274">274</a> }<a name="275" href="#275">275</a> <a name="276" href="#276">276</a> <strong class="jxr_keyword">synchronized</strong> (state) {<a name="277" href="#277">277</a> <strong class="jxr_keyword">return</strong> state.sessionBufferSize;<a name="278" href="#278">278</a> }<a name="279" href="#279">279</a> }<a name="280" href="#280">280</a> <a name="281" href="#281">281</a> @Override<a name="282" href="#282">282</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> onPreAdd(<a name="283" href="#283">283</a> <a href="../../../../../org/apache/mina/common/IoFilterChain.html">IoFilterChain</a> parent, String name, <a href="../../../../../org/apache/mina/common/IoFilter.html">NextFilter</a> nextFilter) <strong class="jxr_keyword">throws</strong> Exception {<a name="284" href="#284">284</a> <strong class="jxr_keyword">if</strong> (!parent.contains(ExecutorFilter.<strong class="jxr_keyword">class</strong>)) {<a name="285" href="#285">285</a> <strong class="jxr_keyword">throw</strong> <strong class="jxr_keyword">new</strong> IllegalStateException(<a name="286" href="#286">286</a> <span class="jxr_string">"At least one "</span> + ExecutorFilter.<strong class="jxr_keyword">class</strong>.getName() + <span class="jxr_string">" must exist in the chain."</span>);<a name="287" href="#287">287</a> }<a name="288" href="#288">288</a> <strong class="jxr_keyword">if</strong> (parent.contains(<strong class="jxr_keyword">this</strong>)) {<a name="289" href="#289">289</a> <strong class="jxr_keyword">throw</strong> <strong class="jxr_keyword">new</strong> IllegalArgumentException(<a name="290" href="#290">290</a> <span class="jxr_string">"You can't add the same filter instance more than once. Create another instance and add it."</span>);<a name="291" href="#291">291</a> }<a name="292" href="#292">292</a> }<a name="293" href="#293">293</a> <a name="294" href="#294">294</a> @Override<a name="295" href="#295">295</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> onPostAdd(<a name="296" href="#296">296</a> <a href="../../../../../org/apache/mina/common/IoFilterChain.html">IoFilterChain</a> parent, String name, <a href="../../../../../org/apache/mina/common/IoFilter.html">NextFilter</a> nextFilter) <strong class="jxr_keyword">throws</strong> Exception {<a name="297" href="#297">297</a> <a name="298" href="#298">298</a> <em class="jxr_comment">// My previous filter must be an ExecutorFilter.</em><a name="299" href="#299">299</a> <a href="../../../../../org/apache/mina/common/IoFilter.html">IoFilter</a> lastFilter = <strong class="jxr_keyword">null</strong>;<a name="300" href="#300">300</a> <strong class="jxr_keyword">for</strong> (IoFilterChain.Entry e: parent.getAll()) {<a name="301" href="#301">301</a> <a href="../../../../../org/apache/mina/common/IoFilter.html">IoFilter</a> currentFilter = e.getFilter();<a name="302" href="#302">302</a> <strong class="jxr_keyword">if</strong> (currentFilter == <strong class="jxr_keyword">this</strong>) {<a name="303" href="#303">303</a> <strong class="jxr_keyword">if</strong> (lastFilter instanceof ExecutorFilter) {<a name="304" href="#304">304</a> <em class="jxr_comment">// Good!</em><a name="305" href="#305">305</a> <strong class="jxr_keyword">break</strong>;<a name="306" href="#306">306</a> } <strong class="jxr_keyword">else</strong> {<a name="307" href="#307">307</a> <strong class="jxr_keyword">throw</strong> <strong class="jxr_keyword">new</strong> IllegalStateException(<a name="308" href="#308">308</a> ReadThrottleFilter.<strong class="jxr_keyword">class</strong>.getName() + <span class="jxr_string">" must be placed after "</span> +<a name="309" href="#309">309</a> <span class="jxr_string">"an "</span> + ExecutorFilter.<strong class="jxr_keyword">class</strong>.getName() + <span class="jxr_string">" in the chain"</span>);<a name="310" href="#310">310</a> }<a name="311" href="#311">311</a> }<a name="312" href="#312">312</a> <a name="313" href="#313">313</a> lastFilter = currentFilter;<a name="314" href="#314">314</a> }<a name="315" href="#315">315</a> <a name="316" href="#316">316</a> <em class="jxr_comment">// Add an entering filter before the ExecutorFilter.</em><a name="317" href="#317">317</a> parent.getEntry(lastFilter).addBefore(name + <span class="jxr_string">".preprocessor"</span>, enterFilter);<a name="318" href="#318">318</a> <a name="319" href="#319">319</a> <strong class="jxr_keyword">int</strong> previousSessionCount = sessionCount.getAndIncrement();<a name="320" href="#320">320</a> <strong class="jxr_keyword">if</strong> (previousSessionCount == 0) {<a name="321" href="#321">321</a> <strong class="jxr_keyword">synchronized</strong> (resumeOthersTask) {<a name="322" href="#322">322</a> resumeOthersFuture = executor.scheduleWithFixedDelay(<a name="323" href="#323">323</a> resumeOthersTask, 3000, 3000, TimeUnit.MILLISECONDS);<a name="324" href="#324">324</a> }<a name="325" href="#325">325</a> }<a name="326" href="#326">326</a> }<a name="327" href="#327">327</a> <a name="328" href="#328">328</a> @Override<a name="329" href="#329">329</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> onPostRemove(<a name="330" href="#330">330</a> <a href="../../../../../org/apache/mina/common/IoFilterChain.html">IoFilterChain</a> parent, String name, <a href="../../../../../org/apache/mina/common/IoFilter.html">NextFilter</a> nextFilter) <strong class="jxr_keyword">throws</strong> Exception {<a name="331" href="#331">331</a> <em class="jxr_comment">// Remove the enter filter together.</em><a name="332" href="#332">332</a> <strong class="jxr_keyword">try</strong> {<a name="333" href="#333">333</a> parent.remove(enterFilter);<a name="334" href="#334">334</a> } <strong class="jxr_keyword">catch</strong> (Exception e) {<a name="335" href="#335">335</a> <em class="jxr_comment">// Ignore.</em><a name="336" href="#336">336</a> }<a name="337" href="#337">337</a> <a name="338" href="#338">338</a> <strong class="jxr_keyword">int</strong> currentSessionCount = sessionCount.decrementAndGet();<a name="339" href="#339">339</a> <strong class="jxr_keyword">if</strong> (currentSessionCount == 0) {<a name="340" href="#340">340</a> <strong class="jxr_keyword">synchronized</strong> (resumeOthersTask) {<a name="341" href="#341">341</a> resumeOthersFuture.cancel(false);<a name="342" href="#342">342</a> resumeOthersFuture = <strong class="jxr_keyword">null</strong>;<a name="343" href="#343">343</a> }<a name="344" href="#344">344</a> }<a name="345" href="#345">345</a> }<a name="346" href="#346">346</a> <a name="347" href="#347">347</a> @Override<a name="348" href="#348">348</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> messageReceived(<a name="349" href="#349">349</a> <a href="../../../../../org/apache/mina/common/IoFilter.html">NextFilter</a> nextFilter, <a href="../../../../../org/apache/mina/common/IoSession.html">IoSession</a> session, Object message) <strong class="jxr_keyword">throws</strong> Exception {<a name="350" href="#350">350</a> exit(session, estimateSize(message));<a name="351" href="#351">351</a> nextFilter.messageReceived(session, message);<a name="352" href="#352">352</a> }<a name="353" href="#353">353</a> <a name="354" href="#354">354</a> @Override<a name="355" href="#355">355</a> <strong class="jxr_keyword">public</strong> <strong class="jxr_keyword">void</strong> filterSetTrafficMask(<a href="../../../../../org/apache/mina/common/IoFilter.html">NextFilter</a> nextFilter, <a href="../../../../../org/apache/mina/common/IoSession.html">IoSession</a> session,<a name="356" href="#356">356</a> <a href="../../../../../org/apache/mina/common/TrafficMask.html">TrafficMask</a> trafficMask) <strong class="jxr_keyword">throws</strong> Exception {<a name="357" href="#357">357</a> <a name="358" href="#358">358</a> <strong class="jxr_keyword">if</strong> (trafficMask.isReadable()) {<a name="359" href="#359">359</a> <a href="../../../../../org/apache/mina/filter/traffic/ReadThrottleFilter.html">State</a> state = getState(session);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -