锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲国产av玩弄放荡人妇,亚洲人成电影青青在线播放,国产亚洲精品xxxhttp://www.tkk7.com/niumd/澶╄鍋ャ佸悰瀛愪互鑷己涓嶆伅 錛涘湴鍔垮潳銆佸悰瀛愪互鍘氬痙杞界墿鈥︹?/description>zh-cnMon, 12 May 2025 00:42:56 GMTMon, 12 May 2025 00:42:56 GMT60Spring JDBC Framework璇﹁В鈥斺旀壒閲廕DBC鎿嶄綔銆丱RM鏄犲皠http://www.tkk7.com/niumd/archive/2011/05/10/349965.html絀虹櫧絀虹櫧Tue, 10 May 2011 13:06:00 GMThttp://www.tkk7.com/niumd/archive/2011/05/10/349965.htmlhttp://www.tkk7.com/niumd/comments/349965.htmlhttp://www.tkk7.com/niumd/archive/2011/05/10/349965.html#Feedback0http://www.tkk7.com/niumd/comments/commentRss/349965.htmlhttp://www.tkk7.com/niumd/services/trackbacks/349965.html

浣滆咃細niumd錛岃漿杞借娉ㄦ槑鍑哄錛岃阿璋?nbsp;  

鍙戣〃鏃墮棿錛?010 騫?nbsp;03 鏈?nbsp;17 鏃?nbsp; 

鍘熸枃閾炬帴錛?a mce_href="/admin/blogs/618449">http://ari.iteye.com/admin/blogs/618449


涓銆丼pring JDBC 姒傝堪

     Spring 鎻愪緵浜嗕竴涓己鏈夊姏鐨勬ā鏉跨被JdbcTemplate綆鍖朖DBC鎿嶄綔錛孌ataSource,JdbcTemplate閮藉彲浠ヤ互Bean鐨勬柟寮忓畾涔夊湪鎯硏ml閰嶇疆鏂囦歡錛孞dbcTemplate鍒涘緩鍙渶娉ㄥ叆涓涓狣ataSource錛屽簲鐢ㄧ▼搴廌ao灞傚彧闇瑕佺戶鎵縅dbcDaoSupport, 鎴栬呮敞鍏dbcTemplate錛屼究鍙互鑾峰彇JdbcTemplate錛孞dbcTemplate鏄竴涓嚎紼嬪畨鍏ㄧ殑綾伙紝澶氫釜Dao鍙互娉ㄥ叆涓涓狫dbcTemplate錛?/p>



<!--         Oracle鏁版嵁婧?          -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@oracle.devcake.co.uk:1521:INTL"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--  set娉ㄥ叆鏂瑰紡鑾峰彇jdbcTemplate -->
<bean id="customerDao" class="JdbcCustomerDao" >
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!-- 娉ㄥ叆dataSource錛宑ustomerDao閫氳繃緇ф壙JdbcDaoSupport ,浣跨敤this.getJdbcTemplate()鑾峰彇JdbcTemplate   -->
<bean id="customerDao" class="JdbcCustomerDao" >
<property name="dataSource" ref="dataSource"/>
</bean>


鐒跺悗灝唈dbcTemplate瀵硅薄娉ㄥ叆鑷畾涔夌殑Dao銆佹垨鑰呯戶鎵縅dbcDaoSupport錛屼緥濡傦細

public class JdbcCustomerDao extends JdbcDaoSupport implements CustomerDao {
}
public class JdbcCustomerDao implements CustomerDao {
private JdbcTemplate jdbcTemplate
public void setJdbcTemplate()JdbcTemplate jdbcTemplate{
this.jdbcTemplate=jdbcTemplate
}
}


浜屻?nbsp;JdbcTemplate 鎻愪緵浠ヤ笅涓昏鏂規硶綆鍖朖DBC鎿嶄綔錛?/strong>

2.1銆丩ist query(String sql,Ojbect[] args,RowMapper rowMapper)

     璇存槑錛氬父鐢ㄧ殑鏌ヨ錛宻ql寰呮墽琛岀殑sql璇彞錛宎rgs鏄痵ql璇彞鐨勫弬鏁幫紝rowMapper璐熻矗灝嗘瘡涓琛岃褰曡漿鍖栦負java瀵硅薄瀛樻斁鍦╨ist錛屽茍鏈緇堣繑鍥烇紝渚嬪錛?/p>

public List<Book> queryByAuthor(String author) {
String sql = "select * from book where author=?";
Collection c = getJdoTemplate().find(sql,
new Object[] { author },new BookRowMapper());
List<Book> books = new ArrayList<Book>();
books.addAll(c);
return books;
}
class BookRowMapper implements RowMapper{
public Object mapRow(ResultSet res, int index) throws SQLException {
Book book = new Book();
book.setId(rs.getInt("id"));
//鐪佺暐set
return book錛?
}
}


   鏇存柊銆佸垹闄ゃ佸叾浠栨煡璇㈡搷浣滅被浼鹼紝涓句緥濡備笅錛岃緇嗙粏鑺傝鍙傝僺pring api錛?/p>


//榪斿洖鍊間負涓涓暱鏁村艦
public long getAverageAge() {
return getJdbcTemplate().queryForLong("SELECT AVG(age) FROM employee");
}
//榪斿洖涓涓暣鏁?
public int getTotalNumberOfEmployees() {
return getJdbcTemplate().queryForInt("SELECT COUNT(0) FROM employees");
}
//鏇存柊鎿嶄綔
this.jdbcTemplate.update(
"insert into t_actor (first_name, surname) values (?, ?)",
new Object[] {"Leonor", "Watling"});



 2.2銆乻pring 2.5鏂板姛鑳斤紝鍙︾被鐨刯dbc ORM錛欱eanPropertyRowMapper


      涓婇潰鎴戜滑媯绱㈡椂蹇呴』瀹炵幇RowMapper錛屽皢緇撴灉闆嗚漿鍖栦負java瀵硅薄銆係pring2.5 綆鍖栦簡榪欎竴鎿嶄綔錛屼嬌寰楁垜浠笉蹇呭啀瀹炵幇RowMapper錛屽疄鐜版鍔熻兘鐨勪咯涓濂囦笢涓滀究鏄細ParameterizedRowMapper錛孭arameterizedBeanPropertyRowMapper錛岃矊浼奸氳繃java鍙嶅皠鏈哄埗瀹炵幇浜嗗皢resultset瀛楁鏄犲皠鍒癹ava瀵硅薄錛屼絾鏄暟鎹〃鐨勫垪蹇呴』鍜宩ava瀵硅薄鐨勫睘鎬у搴旓紝娌℃湁鐮旂┒婧愮爜錛屾湁鐐圭被浼間簬apache 鐨凚eanUtil錛屼笉鐭ヤ負浣曡繖閮ㄥ垎鍦╯pring寮鍙戝弬鑰冩墜鍐屾病鏈夛紝闅鵑亾涓嶆槸緇忓吀銆?/p>



//浣跨敤ParameterizedBeanPropertyRowMapper
@SuppressWarnings({"unchecked"})
public List<Customer> getAll() {
return getJdbcTemplate().query("select * from t_customer", ParameterizedBeanPropertyRowMapper.newInstance(Customer.class));
}
//浣跨敤BeanPropertyRowMapper
@SuppressWarnings({"unchecked"})
public List<Customer> getAll() {
return getJdbcTemplate().query("select * from t_customer", new BeanPropertyRowMapper(Customer.class));
}



娉ㄦ剰錛歅arameterizedBeanPropertyRowMapper鏄疊eanPropertyRowMapper瀛愮被銆傚彟澶栬〃鐨勫瓧孌靛悕縐板繀欏誨拰瀹炰綋綾葷殑鎴愬憳鍙橀噺鍚嶇О涓鑷達紱


2.3銆乻pring涔婮DBC鎵歸噺鎿嶄綔

      jdbcTemplate.batchUpdate(final String[] sql) 錛孉PI瑙i噴錛欼ssue multiple SQL updates on a single JDBC Statement using batching錛岀炕璇戣繃鏉ュぇ鑷翠負錛氳В鍐沖涓猻ql鐨勬彃鍏ャ佹洿鏂般佸垹闄ゆ搷浣滃湪涓涓猄tatement涓傛ц兘涓鑸?/span>

   jdbcTemplate.batchUpdate(String sql, final BatchPreparedStatementSetter pss),綾諱技浜嶫DBC鐨凱reparedStatement錛屾ц兘杈冧笂鐫鏈夋墍鎻愰珮銆?/span>

   鎴戜滑涓句緥璇存槑濡備綍浣跨敤錛岀ず渚嬪涓?

final int count = 2000;
final List<String> firstNames = new ArrayList<String>(count);
final List<String> lastNames = new ArrayList<String>(count);
for (int i = 0; i < count; i++) {
firstNames.add("First Name " + i);
lastNames.add("Last Name " + i);
}
jdbcTemplate.batchUpdate(
"insert into customer (id, first_name, last_name, last_login, comments) values (?, ?, ?, ?, ?)",
new BatchPreparedStatementSetter() {
//涓簆repared statement璁劇疆鍙傛暟銆傝繖涓柟娉曞皢鍦ㄦ暣涓繃紼嬩腑琚皟鐢ㄧ殑嬈℃暟
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setLong(1, i + 10);
ps.setString(2, firstNames.get(i));
ps.setString(3, lastNames.get(i));
ps.setNull(4, Types.TIMESTAMP);
ps.setNull(5, Types.CLOB);
}
//榪斿洖鏇存柊鐨勭粨鏋滈泦鏉℃暟
public int getBatchSize() {
return count;
}
});
}



  BatchSqlUpdate綾繪槸SqlUpdate 鐨勫瓙綾伙紝閫傜敤浜庢彃鍏ャ佸垹闄ゃ佹洿鏂版壒閲忔搷浣滐紝鍐呴儴浣跨敤PreparedStatement錛屾墍浠ユ晥鐜囧緢楂橈紝鎵歸噺璇彞杈懼埌璁懼畾鐨刡atchSize錛屾垨鑰呮墜鍔ㄨ皟鐢╢lush鎵嶄細鎵ц鎵歸噺鎿嶄綔銆傛敞鎰忥細姝ょ被鏄潪綰跨▼瀹夊叏鐨勶紝蹇呴』涓烘瘡涓嬌鐢ㄨ呭垱寤轟竴涓疄渚嬶紝鎴栬呭湪鍚屼竴涓嚎紼嬩腑浣跨敤鍓嶈皟鐢╮eset銆?/span>

   涓嬮潰鎴戜滑涓句緥璇存槑濡備綍浣跨敤BatchSqlUpdate錛屾潵鎵ц鎵歸噺鎿嶄綔銆傜ず渚嬪涓嬶細

class BatchInsert extends BatchSqlUpdate {
private static final String SQL = "insert into t_customer (id, first_name, last_name, last_login, "
+ "comments) values (?, ?, ?, ?, null)";
BatchInsert(DataSource dataSource) {
super(dataSource, SQL);
declareParameter(new SqlParameter(Types.INTEGER));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.VARCHAR));
declareParameter(new SqlParameter(Types.TIMESTAMP));
setBatchSize(10);
}
}


int count = 5000;
for (int i = 0; i < count; i++) {
batchInsert.update(new Object[] { i + 100L, "a" + i, "b" + i, null });
}


 鑷蟲錛宻pring JDBC涓昏鐨勫簲鐢ㄥ熀鏈笂閮界畝鍗曠綏鍒椾竴鐣紝鎵鏈変唬鐮佸潎涓烘枃绔犱婦渚嬶紝涓嶆槸寰堜弗璋紝浠呬負婕旂ず姣忎竴縐嶇敤娉曪紝鎶涚爾寮曠帀錛屽笇鏈涙湁鐙壒瑙佽В鐨勬媿鐮栵紝鏈夐棶棰樼殑璇鋒寚鏄庨棶棰樻墍鍦?璋㈣阿



絀虹櫧 2011-05-10 21:06 鍙戣〃璇勮
]]>
Tomcat6.x鐩綍涓巗erver.xml璇﹁Вhttp://www.tkk7.com/niumd/archive/2011/05/10/349964.html絀虹櫧絀虹櫧Tue, 10 May 2011 13:04:00 GMThttp://www.tkk7.com/niumd/archive/2011/05/10/349964.htmlhttp://www.tkk7.com/niumd/comments/349964.htmlhttp://www.tkk7.com/niumd/archive/2011/05/10/349964.html#Feedback0http://www.tkk7.com/niumd/comments/commentRss/349964.htmlhttp://www.tkk7.com/niumd/services/trackbacks/349964.html闃呰鍏ㄦ枃

絀虹櫧 2011-05-10 21:04 鍙戣〃璇勮
]]>
欏圭洰閲嶆瀯涔嬪懡浠ゆā寮?/title><link>http://www.tkk7.com/niumd/archive/2011/05/10/349962.html</link><dc:creator>絀虹櫧</dc:creator><author>絀虹櫧</author><pubDate>Tue, 10 May 2011 13:03:00 GMT</pubDate><guid>http://www.tkk7.com/niumd/archive/2011/05/10/349962.html</guid><wfw:comment>http://www.tkk7.com/niumd/comments/349962.html</wfw:comment><comments>http://www.tkk7.com/niumd/archive/2011/05/10/349962.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.tkk7.com/niumd/comments/commentRss/349962.html</wfw:commentRss><trackback:ping>http://www.tkk7.com/niumd/services/trackbacks/349962.html</trackback:ping><description><![CDATA[<div style="color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; margin-top: 8px; margin-right: 8px; margin-bottom: 8px; margin-left: 8px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; background-position: initial initial; background-repeat: initial initial; "> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">    欏圭洰涓湁涓笟鍔″鐞嗙被澶у皬117K錛屼唬鐮?700琛岋紝鐪嬫綾誨樊鐐歸浄姝繪垜錛屽浠婂瑕佸鍔犱笟鍔¢昏緫澶х害20涓惂錛屾綾誨鏋滈殢鐫欏圭洰宸ョ▼鐨勪簩鏈熴佷笁鏈熷嬈℃坊鍔犻昏緫榪熸棭鏈変竴澶╁ぇ灝忚揪鍒癕錛屽櫌銆乵ygod銆傜粏蹇冪爺璇誨墠浜虹殑宸ヤ綔鎬葷粨錛屽彂鐜板叾涓湁鐐瑰彲鏀歸犵殑铔涗笣椹抗(鏈漢寰堢銆佸埆絎戞垜鎵嶅彂鐜板浣曟敼閫?銆?/p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">     涓嬮潰鎴戜滑瀵逛笟鍔℃祦紼嬨佷互鍙婃秹鍙婄殑鐩稿叧綾昏繘琛屼粙緇嶏紝<span lang="EN-US"><span mce_style="font-family: Calibri;" style="font-family: Calibri; ">Msg</span>浠h〃鎺ュ彈鍒板鎴風鐨勪竴涓秷鎭姤鏂囷紝娑堟伅鎶ユ枃緇撴瀯錛氭秷鎭ご<span lang="EN-US"><span mce_style="font-family: Calibri;" style="font-family: Calibri; ">+</span></span>娑堟伅浣擄紝娑堟伅澶村弬鏁板浐瀹氥佹秷鎭綋鍙傛暟涓嶅畾錛屼笅闈㈡槸涓涓畝鍗曠殑綾誨浘錛岃繖鍙槸涓涓ā鎷熷満鏅紝<span lang="EN-US"><span mce_style="font-family: Calibri;" style="font-family: Calibri; ">****Req</span></span>浠h〃鍚勬埛绔姹傜被錛?span lang="EN-US"><span mce_style="font-family: Calibri;" style="font-family: Calibri; ">***Rsp</span></span>浠h〃榪斿洖緇欏鎴風鐨勫弬鏁扮被銆傚疄闄呮瘮姝ゅ鏉傦紝涓烘弿榪伴棶棰樻垜浠畝鍗曟憳闄ゅ嚑涓被浠嬬粛錛屽埆闂垜涓轟綍榪欎箞璁捐緇ф壙銆傜被鍥?span lang="EN-US"><span mce_style="font-family: Calibri;" style="font-family: Calibri; ">msg</span></span>涓?span lang="EN-US"><span mce_style="font-family: Calibri;" style="font-family: Calibri; ">msgHead</span></span>鏄粍鍚堝叧緋諱篃璁哥敾閿欎簡銆佷笉褰撲箣澶勮鎸囧嚭錛屽嬁鎭惰鍚戜激錛?/span></p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span lang="EN-US"> </span></p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "> <br /> <img src="http://dl.iteye.com/upload/attachment/264052/9e823571-e81c-35d0-a4e5-b5b14b525542.png" mce_src="http://dl.iteye.com/upload/attachment/264052/9e823571-e81c-35d0-a4e5-b5b14b525542.png" alt="" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /><br />  澶勭悊璇鋒眰Handler綾葷殑浠g爜閫昏緫濡備笅錛?/p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <pre name="code" class="java" style="color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; margin-top: 0px; margin-right: 5px; margin-bottom: 5px; margin-left: 15px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #fafafa; background-position: initial initial; background-repeat: initial initial; ">//綾諱腑涓昏鏂規硶濡備笅 public void execute(Object object) { Message message = (Message)object; int opcode = message.getOpcode(); int connectId = message.getConnectId(); //娑堟伅澶村凡緇忚В鏋愶紝鑾峰彇娑堟伅浣擄紝鍗沖瓙綾誨睘鎬у瓧鑺傛暟緇? byte[] bytes = message.getBytes(); if (opcode == MsgInfo.ADD_RING) { // 璁㈣喘褰╅搩 orderRing(connectId, bytes); } else if (opcode == MsgInfo.PRESENT_RING) { // 璧犻佸僵閾? presentRing(connectId, bytes); } else if (opcode == MsgInfo.DEL_RING) { // 鍒犻櫎涓漢閾冮煶 delPersonalRing(connectId, bytes); } //姝ゅ鐪佺暐n涓猠lse if } //鍏朵粬鍒犻櫎銆佽禒閫佷笌鐪佺暐鐨別lse if涓殑澶勭悊閫昏緫涓庝箣鍩烘湰鐩稿悓 private void orderRing(int connectId, byte[] bytes) { //澶勭悊鏂規硶鍒嗕負鍥涙錛屽叿浣撲唬鐮佺渷鐣? //1銆佽В鏋愬瓧鑺傛暟緇勪負璁㈣喘閾冮煶綾? //2銆佸鐞嗚璐叧緋? //3銆佸鐞嗙粨鏋滃皝瑁呬負璁㈣喘鍝嶅簲綾? //4銆佸彂閫佸洖瀹㈡埛绔? } //鐪佺暐presentRing銆乨elPersonalRing絳変竴緋誨垪鍏朵粬鏂規硶錛屾墍鏈夌殑澶勭悊鏂規硶鍙傛暟鐩稿悓…… </pre> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">    閴翠簬姝ゃ佹兂鍒頒嬌鐢ㄥ懡浠ゆā寮忔敼閫犳綾伙紝濡傛灉涓嶄簡瑙e懡浠ゆā寮忚闃呰鐩稿叧涔︾睄錛屽ぇ璇濊璁℃ā寮忔垨璁捐涓庢ā寮忥紝榪欓噷鎴戜滑浠呯粰鍑哄ぇ鑷寸殑瀹氫簬涓庣被鍥俱?/p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">浣曡皳鍛戒護妯″紡錛氬皢涓涓姹傚皝瑁呬負涓涓璞★紝浠庤屾槸浣犲彲鐢ㄤ笉鍚岀殑璇鋒眰瀵瑰鎴風鍙傛暟鍖栵紝瀵硅姹傛帓闃熸垨璁板綍鏃ュ織錛屼互鍙婃敮鎸佸彲鎾ら攢鐨勬搷浣溿?/p> <p class="MsoNormal" mce_style="text-indent: 15.75pt; margin: 0cm 0cm 0pt;" style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 15.75pt; "> <span lang="EN-US"><span mce_style="font-family: Calibri;" style="font-family: Calibri; ">Shit</span></span>銆佽繖鍙ヨ瘽寰堥毦鐞嗚В鍝︼紝閭e氨鍏堝埆鐞嗚В浜嗭紝鎴戜滑鐪嬩笅鍛戒護妯″紡鐨勭被鍥撅紝鐒跺悗浠嬬粛濡備綍浣跨敤鍛戒護妯″紡鏀歸犱笂闈㈢殑<span lang="EN-US"><span mce_style="font-family: Calibri;" style="font-family: Calibri; ">elseif</span></span>銆?/p> <p class="MsoNormal" mce_style="text-indent: 21pt; margin: 0cm 0cm 0pt;" style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21pt; ">綾誨浘鍏堢渷鐣ワ紝涓婄彮鍋風┖鍐欑殑錛?/p> <p class="MsoNormal" mce_style="text-indent: 21pt; margin: 0cm 0cm 0pt;" style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21pt; "><span lang="EN-US"><span mce_style="font-family: Calibri;" style="font-family: Calibri; "> </span></span></p> <p class="MsoNormal" mce_style="text-indent: 21pt; margin: 0cm 0cm 0pt;" style="margin-top: 0cm; margin-right: 0cm; margin-bottom: 0pt; margin-left: 0cm; text-indent: 21pt; ">涓嬮潰榪涘叆姝i錛屽<span lang="EN-US"><span mce_style="font-family: Calibri;" style="font-family: Calibri; ">Handler</span></span>灝忔墜鏈紑濮嬶紝涓昏鑰冭檻濡備笅錛?/p> <p class="MsoListParagraph" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><strong><span lang="EN-US"><span mce_style="font-family: Calibri;" style="font-family: Calibri; ">1銆?/span></span>鎻愮偧鏂規硶</strong></p> <p class="MsoListParagraph" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">     灝嗘瘡涓?span lang="EN-US"><span mce_style="font-family: Calibri;" style="font-family: Calibri; ">if</span></span>璇彞鍧椾腑鐨勯昏緫鎻愬彇涓轟竴涓柟娉曪紝榪欓噷鎴戜滑鐨?span lang="EN-US"><span mce_style="font-family: Calibri;" style="font-family: Calibri; ">handler</span></span>宸茬粡瀹炵幇錛屽氨鏄細<span mce_style="font-family: 'Courier New'; color: black; font-size: 10pt;" style="font-family: 'Courier New'; color: black; font-size: 10pt; ">orderRing</span>銆?span mce_style="font-family: 'Courier New'; color: black; font-size: 10pt;" style="font-family: 'Courier New'; color: black; font-size: 10pt; ">presentRing</span>銆?span mce_style="font-family: 'Courier New'; color: black; font-size: 10pt;" style="font-family: 'Courier New'; color: black; font-size: 10pt; ">delPersonalRing</span>銆?#8230;…銆?/p> <p class="MsoListParagraph" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><strong><span lang="EN-US"><span mce_style="font-family: Calibri;" style="font-family: Calibri; ">2銆?/span></span>鎻愮偧綾?/strong></p> <p class="MsoListParagraph" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">  灝嗘瘡涓笟鍔″鐞嗘柟娉曟彁鍙栦負浠ュ悇綾伙紝鐒跺悗瀵瑰叿浣撶被榪涜鎶借薄錛屾彁鍙栫埗綾繪垨鑰呮帴鍙o紱浠g爜濡備笅錛?/p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <pre name="code" class="java" style="color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; margin-top: 0px; margin-right: 5px; margin-bottom: 5px; margin-left: 15px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #fafafa; background-position: initial initial; background-repeat: initial initial; ">public Abstract class Command{ public void execute() 锝? public class OrderRingCommand extends Command { private Handler hander; public OrderRingCommand(Handler hander){ this.hander = hander; } public void execute(int connectId, byte[] bytes){ //1銆佽В鏋愬瓧鑺傛暟緇勪負璁㈣喘閾冮煶綾? //2銆佸鍔犺璐叧緋? //3銆佸鐞嗙粨鏋滃皝瑁呬負璁㈣喘鍝嶅簲綾? //4銆佸彂閫佸洖瀹㈡埛绔? } /** * 1銆佽В鏋愬瓧鑺傛暟緇勪負璁㈣喘閾冮煶綾? */ public void method1(){ } /** * 2銆佸鐞嗚璐叧緋? */ public void method2(){ } /** * 3銆佸鐞嗙粨鏋滃皝瑁呬負璁㈣喘鍝嶅簲綾? */ public void method3(){ } /** * 4銆佺粨鏋滃彂閫佸洖瀹㈡埛绔? */ public void method4(){ } } public class DelRingCommand extends Command { private Handler hander; public DelRingCommand(Handler hander){ this.hander = hander; } public void execute(int connectId, byte[] bytes){ //1銆佽В鏋愬瓧鑺傛暟緇勪負璁㈣喘閾冮煶綾? //2銆佸垹闄よ喘鍏崇郴 //3銆佸鐞嗙粨鏋滃皝瑁呬負璁㈣喘鍝嶅簲綾? //4銆佸彂閫佸洖瀹㈡埛绔? } //鎻愬彇鏂規硶 } </pre> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "> <span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "> </span></span></span></span></p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><strong><span lang="EN-US"><span mce_style="font-family: Calibri;" style="font-family: Calibri; ">3銆?/span></span>鍛戒護妯″紡鏀歸犳浛鎹?/strong><span lang="EN-US"><span mce_style="font-family: Calibri;" style="font-family: Calibri; "><strong>elseif錛?/strong></span></span></p> <p class="MsoListParagraph" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <pre name="code" class="java" style="color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; margin-top: 0px; margin-right: 5px; margin-bottom: 5px; margin-left: 15px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #cccccc; border-right-color: #cccccc; border-bottom-color: #cccccc; border-left-color: #cccccc; padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #fafafa; background-position: initial initial; background-repeat: initial initial; ">Map<Integer, Command> map = new HashMap<Integer,Command>(); static{ map.put(MsgInfo.ADD_RING, new OrderRingCommand()); //鐪佸嵈鍏朵粬錛岃繖閲屼粎涓烘紨紺猴紝瀹為檯欏圭洰涓疄渚嬪寲綾婚氳繃spring瀹瑰櫒鎴栬呭叾浠栨柟娉? } public void execute(Object object) { Message message = (Message)object; int opcode = message.getOpcode(); int connectId = message.getConnectId(); //娑堟伅澶村凡緇忚В鏋愶紝鑾峰彇娑堟伅浣擄紝鍗沖瓙綾誨睘鎬у瓧鑺傛暟緇? byte[] bytes = message.getBytes(); map.get(opcode).execute(connectId,bytes); } </pre> <p class="MsoListParagraph" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "></span></span></span></span></span></span></span></span></span></span></span></p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">     <span lang="EN-US"></span></p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "></span></span></span></span></span></span></span></p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span lang="EN-US"></span></p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span lang="EN-US"> 鍛戒護妯″紡鏇挎崲else if浠g爜鍧忓懗閬撶殑閲嶆瀯緇撴潫錛屼紬澶氱殑if鏉′歡鍧楃儫娑堜簯鏁o紝鍙栬屼唬涔嬬殑鏄竴涓釜綺劇畝鐨勭被錛宒oc鐗堟湰鍦ㄩ檮浠朵腑</span></p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span lang="EN-US"><font><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><font mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "></font></span></span></span></span></span></span></font></span></p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font><font mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><br /> </font></font></p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <p class="MsoListParagraph" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <p> </p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <p class="MsoListParagraph" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <p> </p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "><span mce_style="font-size: 10.5pt;" style="font-size: 10.5pt; "></span></span></span></span></span></span></p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <p class="MsoListParagraph" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "></p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <p class="MsoListParagraph" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><br /> </p> </div> <img src ="http://www.tkk7.com/niumd/aggbug/349962.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.tkk7.com/niumd/" target="_blank">絀虹櫧</a> 2011-05-10 21:03 <a href="http://www.tkk7.com/niumd/archive/2011/05/10/349962.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>娣卞叆嫻呭嚭ThreadLocalhttp://www.tkk7.com/niumd/archive/2011/05/10/349961.html絀虹櫧絀虹櫧Tue, 10 May 2011 13:02:00 GMThttp://www.tkk7.com/niumd/archive/2011/05/10/349961.htmlhttp://www.tkk7.com/niumd/comments/349961.htmlhttp://www.tkk7.com/niumd/archive/2011/05/10/349961.html#Feedback0http://www.tkk7.com/niumd/comments/commentRss/349961.htmlhttp://www.tkk7.com/niumd/services/trackbacks/349961.html

涓銆乀hreadLocal姒傝堪

       瀛︿範JDK涓殑綾伙紝棣栧厛鐪嬩笅JDK API瀵規綾葷殑鎻忚堪錛屾弿榪板涓嬶細

JDK API 鍐欓亾
璇ョ被鎻愪緵浜嗙嚎紼嬪眬閮?(thread-local) 鍙橀噺銆傝繖浜涘彉閲忎笉鍚屼簬瀹冧滑鐨勬櫘閫氬搴旂墿錛屽洜涓鴻闂煇涓彉閲忥紙閫氳繃鍏?get 鎴?set 鏂規硶錛夌殑姣忎釜綰跨▼閮芥湁鑷繁鐨勫眬閮ㄥ彉閲忥紝瀹冪嫭绔嬩簬鍙橀噺鐨勫垵濮嬪寲鍓湰銆俆hreadLocal 瀹炰緥閫氬父鏄被涓殑 private static 瀛楁錛屽畠浠笇鏈涘皢鐘舵佷笌鏌愪竴涓嚎紼嬶紙渚嬪錛岀敤鎴?ID 鎴栦簨鍔?ID錛夌浉鍏寵仈銆?nbsp;

    API琛ㄨ揪浜嗕笅闈㈠嚑縐嶈鐐癸細

1銆乀hreadLocal涓嶆槸綰跨▼錛屾槸綰跨▼鐨勪竴涓彉閲忥紝浣犲彲浠ュ厛綆鍗曠悊瑙d負綰跨▼綾葷殑灞炴у彉閲忋?/p>

2銆乀hreadLocal 鍦ㄧ被涓氬父瀹氫箟涓洪潤鎬佺被鍙橀噺銆?/p>

3銆佹瘡涓嚎紼嬫湁鑷繁鐨勪竴涓猅hreadLocal錛屽畠鏄彉閲忕殑涓涓?#8216;鎷瘋礉’錛屼慨鏀瑰畠涓嶅獎鍝嶅叾浠栫嚎紼嬨?/p>


    鏃㈢劧瀹氫箟涓虹被鍙橀噺錛屼負浣曚負姣忎釜綰跨▼緇存姢涓涓壇鏈紙濮戜笖鎴愪負‘鎷瘋礉’瀹規槗鐞嗚В錛夛紝璁╂瘡涓嚎紼嬬嫭绔嬭闂紵澶氱嚎紼嬬紪紼嬬殑緇忛獙鍛婅瘔鎴戜滑錛屽浜庣嚎紼嬪叡浜祫婧愶紙浣犲彲浠ョ悊瑙d負灞炴э級錛岃祫婧愭槸鍚﹁鎵鏈夌嚎紼嬪叡浜紝涔熷氨鏄榪欎釜璧勬簮琚竴涓嚎紼嬩慨鏀規槸鍚﹀獎鍝嶅彟涓涓嚎紼嬬殑榪愯錛屽鏋滃獎鍝嶆垜浠渶瑕佷嬌鐢╯ynchronized鍚屾錛岃綰跨▼欏哄簭璁塊棶銆?/p>


   ThreadLocal閫傜敤浜庤祫婧愬叡浜絾涓嶉渶瑕佺淮鎶ょ姸鎬佺殑鎯呭喌錛屼篃灝辨槸涓涓嚎紼嬪璧勬簮鐨勪慨鏀癸紝涓嶅獎鍝嶅彟涓涓嚎紼嬬殑榪愯錛涜繖縐嶈璁℃槸‘絀洪棿鎹㈡椂闂?#8217;錛宻ynchronized欏哄簭鎵ц鏄?strong>‘鏃墮棿鎹㈠彇絀洪棿’銆?/p>


浜屻乀hreadLocal鏂規硶浠嬬粛



 T get() 
          榪斿洖姝ょ嚎紼嬪眬閮ㄥ彉閲忕殑褰撳墠綰跨▼鍓湰涓殑鍊箋?/td>
protected  T initialValue() 
          榪斿洖姝ょ嚎紼嬪眬閮ㄥ彉閲忕殑褰撳墠綰跨▼鐨?#8220;鍒濆鍊?#8221;銆?/td>
 void remove() 
          縐婚櫎姝ょ嚎紼嬪眬閮ㄥ彉閲忓綋鍓嶇嚎紼嬬殑鍊箋?/td>
 void set(T value) 
          灝嗘綰跨▼灞閮ㄥ彉閲忕殑褰撳墠綰跨▼鍓湰涓殑鍊艱緗負鎸囧畾鍊箋?/td>


涓夈佹繁鍏ユ簮鐮?/strong>

    ThreadLocal鏈変竴涓猅hreadLocalMap闈欐佸唴閮ㄧ被錛屼綘鍙互綆鍗曠悊瑙d負涓涓狹AP錛岃繖涓?#8216;Map’涓烘瘡涓嚎紼嬪鍒朵竴涓彉閲忕殑‘鎷瘋礉’瀛樺偍鍏朵腑銆?/p>

    褰撶嚎紼嬭皟鐢═hreadLocal.get()鏂規硶鑾峰彇鍙橀噺鏃?棣栧厛鑾峰彇褰撳墠綰跨▼寮曠敤錛屼互姝や負key鍘昏幏鍙栧搷搴旂殑ThreadLocalMap錛屽鏋滄‘Map’涓嶅瓨鍦ㄥ垯鍒濆鍖栦竴涓紝鍚﹀垯榪斿洖鍏朵腑鐨勫彉閲忥紝浠g爜濡備笅錛?/p>


 public T get() {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null)
return (T)e.value;
}
return setInitialValue();
}

    璋冪敤get鏂規硶濡傛灉姝ap涓嶅瓨鍦ㄩ鍏堝垵濮嬪寲錛屽垱寤烘map錛屽皢綰跨▼涓簁ey錛屽垵濮嬪寲鐨剉laue瀛樺叆鍏朵腑錛屾敞鎰忔澶勭殑initialValue錛屾垜浠彲浠ヨ鐩栨鏂規硶錛屽湪棣栨璋冪敤鏃跺垵濮嬪寲涓涓傚綋鐨勫箋俿etInitialValue浠g爜濡備笅錛?/p>

    private T setInitialValue() {
T value = initialValue();
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
return value;
}


    set鏂規硶鐩稿姣旇緝綆鍗曞鏋滅悊瑙d互涓婁咯涓柟娉曪紝鑾峰彇褰撳墠綰跨▼鐨勫紩鐢紝浠巑ap涓幏鍙栬綰跨▼瀵瑰簲鐨刴ap錛屽鏋渕ap瀛樺湪鏇存柊緙撳瓨鍊鹼紝鍚﹀垯鍒涘緩騫跺瓨鍌紝浠g爜濡備笅錛?/p>

    public void set(T value) {
Thread t = Thread.currentThread();
ThreadLocalMap map = getMap(t);
if (map != null)
map.set(this, value);
else
createMap(t, value);
}


    瀵逛簬ThreadLocal鍦ㄤ綍澶勫瓨鍌ㄥ彉閲忓壇鏈紝鎴戜滑鐪媑etMap鏂規硶錛氳幏鍙栫殑鏄綋鍓嶇嚎紼嬬殑ThreadLocal綾誨瀷鐨則hreadLocals灞炴с傛樉鐒跺彉閲忓壇鏈瓨鍌ㄥ湪姣忎竴涓嚎紼嬩腑銆?/p>


/**
* 鑾峰彇綰跨▼鐨凾hreadLocalMap 灞炴у疄渚?
*/
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}


    涓婇潰鎴戜滑鐭ラ亾鍙橀噺鍓湰瀛樻斁浜庝綍澶勶紝榪欓噷鎴戜滑綆鍗曡涓嬪浣曡java鐨勫瀮鍦炬敹闆嗘満鍒舵敹闆嗭紝褰撴垜浠笉鍦ㄤ嬌鐢ㄦ槸璋冪敤set(null)錛屾鏃朵笉鍦ㄥ皢寮曠敤鎸囧悜璇?#8216;map’錛岃岀嚎紼嬮鍑烘椂浼氭墽琛岃祫婧愬洖鏀舵搷浣滐紝灝嗙敵璇風殑璧勬簮榪涜鍥炴敹錛屽叾瀹炲氨鏄皢灞炴х殑寮曠敤璁劇疆涓簄ull銆傝繖鏃跺凡緇忎笉鍦ㄦ湁浠諱綍寮曠敤鎸囧悜璇ap錛屾晠鑰屼細琚瀮鍦炬敹闆嗐?/p>


 鍥涖乀hreadLocal搴旂敤紺轟緥


      鍦ㄦ垜鐨勫彟涓綃囨枃绔狅紝瀵筎hreadLocal鐨勪嬌鐢ㄥ仛浜嗕竴涓疄渚嬶紝姝ょず渚嬩篃鍙互鐢ㄤ綔鐢熶駭鐜錛岃鍙傝錛?a mce_href="/blog/757641">http://ari.iteye.com/blog/757641



濡傛湁闂璇風暀璦璁ㄨ錛岃阿璋?/p>

絀虹櫧 2011-05-10 21:02 鍙戣〃璇勮
]]>
ThreadLocal紺轟緥http://www.tkk7.com/niumd/archive/2011/05/10/349960.html絀虹櫧絀虹櫧Tue, 10 May 2011 13:01:00 GMThttp://www.tkk7.com/niumd/archive/2011/05/10/349960.htmlhttp://www.tkk7.com/niumd/comments/349960.htmlhttp://www.tkk7.com/niumd/archive/2011/05/10/349960.html#Feedback0http://www.tkk7.com/niumd/comments/commentRss/349960.htmlhttp://www.tkk7.com/niumd/services/trackbacks/349960.html

    鏈枃鍊熻姳鐚經錛屽紩鐢═im Cull鐨勫崥鏂?#8220;SimpleDateFormat: Performance Pig”浠嬬粛涓婽hreadLocal鐨勭畝鍗曚嬌鐢紝鍚屾椂涔熷SimpleDateFormat鐨勪嬌鐢ㄦ湁涓繁鍏ョ殑浜嗚В銆?/p>

Tim Cull 鍐欓亾
Just yesterday I came across this problem “in the wild” for the third time in my career so far: an application with performance problems creating tons of java.text.SimpleDateFormat instances. So, I have to get this out there: creating a new instance of SimpleDateFormat is incredibly expensive and should be minimized. In the case that prompted this post, I was using JProfiler to profile this code that parses a CSV file and discovered that 50% of the time it took to suck in the file and make 55,000 objects out of it was spent solely in the constructor of SimpleDateFormat. It created and then threw away a new one every time it had to parse a date. Whew! 

“Great,” you think, “I’ll just create one, static instance, slap it in a field in a DateUtils helper class and life will be good.” 

Well, more precisely, life will be good about 97% of the time. A few days after you roll that code into production you’ll discover the second cool fact that’s good to know: SimpleDateFormat is not thread safe. Your code will work just fine most of the time and all of your regression tests will probably pass, but once your system gets under a production load you’ll see the occasional exception. 

“Fine,” you think, “I’ll just slap a ’synchronized’ around my use of that one, static instance.” 

Ok, fine, you could do that and you’d be more or less ok, but the problem is that you’ve now taken a very common operation (date formatting and parsing) and crammed all of your otherwise-lovely, super-parallel application through a single pipe to get it done.


     澶ц嚧鎰忔濓細Tim Cull紕板埌涓涓猄impleDateFormat甯︽潵鐨勪弗閲嶇殑鎬ц兘闂錛岃闂涓昏鏈塖impleDateFormat寮曞彂錛屽垱寤轟竴涓猄impleDateFormat瀹炰緥鐨勫紑閿姣旇緝鏄傝吹錛岃В鏋愬瓧絎︿覆鏃墮棿鏃墮綣佸垱寤虹敓鍛藉懆鏈熺煭鏆傜殑瀹炰緥瀵艱嚧鎬ц兘浣庝笅銆傚嵆浣垮皢SimpleDateFormat瀹氫箟涓洪潤鎬佺被鍙橀噺錛岃矊浼艱兘瑙e喅榪欎釜闂錛屼絾鏄疭impleDateFormat鏄潪綰跨▼瀹夊叏鐨勶紝鍚屾牱瀛樺湪闂錛屽鏋滅敤‘synchronized’綰跨▼鍚屾鍚屾牱闈復闂錛屽悓姝ュ鑷存ц兘涓嬮檷錛堢嚎紼嬩箣闂村簭鍒楀寲鐨勮幏鍙朣impleDateFormat瀹炰緥錛夈?/p>

    Tim Cull浣跨敤Threadlocal瑙e喅浜嗘闂錛屽浜庢瘡涓嚎紼婼impleDateFormat涓嶅瓨鍦ㄥ獎鍝嶄粬浠箣闂村崗浣滅殑鐘舵侊紝涓烘瘡涓嚎紼嬪垱寤轟竴涓猄impleDateFormat鍙橀噺鐨勬嫹璐濇垨鑰呭彨鍋氬壇鏈紝浠g爜濡備笅錛?/p>


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 浣跨敤ThreadLocal浠ョ┖闂存崲鏃墮棿瑙e喅SimpleDateFormat綰跨▼瀹夊叏闂銆?
* @author
*
*/
public class DateUtil {
private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
@SuppressWarnings("rawtypes")
private static ThreadLocal threadLocal = new ThreadLocal() {
protected synchronized Object initialValue() {
return new SimpleDateFormat(DATE_FORMAT);
}
};
public static DateFormat getDateFormat() {
return (DateFormat) threadLocal.get();
}
public static Date parse(String textDate) throws ParseException {
return getDateFormat().parse(textDate);
}
}


   鍒涘緩涓涓猅hreadLocal綾誨彉閲忥紝榪欓噷鍒涘緩鏃剁敤浜嗕竴涓尶鍚嶇被錛岃鐩栦簡initialValue鏂規硶錛屼富瑕佷綔鐢ㄦ槸鍒涘緩鏃跺垵濮嬪寲瀹炰緥銆備篃鍙互閲囩敤涓嬮潰鏂瑰紡鍒涘緩錛?/p>


//絎竴嬈¤皟鐢╣et灝嗚繑鍥瀗ull
private static ThreadLocal threadLocal = new ThreadLocal()錛?
//鑾峰彇綰跨▼鐨勫彉閲忓壇鏈紝濡傛灉涓嶈鐩杋nitialValue錛岀涓嬈et榪斿洖null錛屾晠闇瑕佸垵濮嬪寲涓涓猄impleDateFormat錛屽茍set鍒皌hreadLocal涓?
public static DateFormat getDateFormat()
{
DateFormat df = (DateFormat) threadLocal.get();
if(df==null){
df = new SimpleDateFormat(DATE_FORMAT)
threadLocal.set(df);
}
return df;
}


   鎴戜滑鐪嬩笅鎴戜滑瑕嗙洊鐨刬nitialValue鏂規硶錛?/p>


protected T initialValue() {
return null;//鐩存帴榪斿洖null
}






絀虹櫧 2011-05-10 21:01 鍙戣〃璇勮
]]>
鎺㈢┒Struts2榪愯鏈哄埗錛歋trutsPrepareAndExecuteFilter 婧愮爜鍓栨瀽http://www.tkk7.com/niumd/archive/2011/05/10/349959.html絀虹櫧絀虹櫧Tue, 10 May 2011 12:58:00 GMThttp://www.tkk7.com/niumd/archive/2011/05/10/349959.htmlhttp://www.tkk7.com/niumd/comments/349959.htmlhttp://www.tkk7.com/niumd/archive/2011/05/10/349959.html#Feedback4http://www.tkk7.com/niumd/comments/commentRss/349959.htmlhttp://www.tkk7.com/niumd/services/trackbacks/349959.html

  浣滆咃細niumd

  blog錛?a mce_href="/">http://ari.iteye.com

 涓銆佹榪?/strong>

     Struts2鐨勬牳蹇冩槸涓涓狥ilter錛孉ction鍙互鑴辯web瀹瑰櫒錛岄偅涔堟槸浠涔堣http璇鋒眰鍜宎ction鍏寵仈鍦ㄤ竴璧風殑錛屼笅闈㈡垜浠繁鍏ユ簮鐮佹潵鍒嗘瀽涓婼truts2鏄浣曞伐浣滅殑銆?/p>

FilterDispatcher API 鍐欓亾
Deprecated. Since Struts 2.1.3, use StrutsPrepareAndExecuteFilter instead or StrutsPrepareFilter and StrutsExecuteFilter if needing using the ActionContextCleanUp filter in addition to this one


     閴翠簬甯歌鎯呭喌瀹樻柟鎺ㄨ崘浣跨敤StrutsPrepareAndExecuteFilter鏇夸唬FilterDispatcher錛屾垜浠鏂囧皢鍓栨瀽StrutsPrepareAndExecuteFilter錛屽叾鍦ㄥ伐紼嬩腑浣滀負涓涓狥ilter閰嶇疆鍦╳eb.xml涓紝閰嶇疆濡備笅錛?/p>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


浜屻佹簮鐮佸睘鎬ф柟娉曠畝浠?/strong>

    涓嬮潰鎴戜滑鐮旂┒涓婼trutsPrepareAndExecuteFilter婧愮爜錛岀被鐨勪富瑕佷俊鎭涓嬶細


灞炴ф憳瑕?/strong>
protected  List<Pattern> excludedPatterns 
           
protected  ExecuteOperations execute 
           
protected  PrepareOperations prepare 
           


    StrutsPrepareAndExecuteFilter涓庢櫘閫氱殑Filter騫舵棤鍖哄埆錛屾柟娉曢櫎緇ф壙鑷狥ilter澶栵紝浠呮湁涓涓洖璋冩柟娉曪紝絎笁閮ㄥ垎鎴戜滑灝嗘寜鐓ilter鏂規硶璋冪敤欏哄簭錛岀敱init鈥?gt;doFilter鈥?gt;destroy欏哄簭鍦板垎鏋愭簮鐮併?/p>
鏂規硶鎽樿
 void destroy() 
           緇ф壙鑷狥ilter錛岀敤浜庤祫婧愰噴鏀?/td>
 void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) 
           緇ф壙鑷狥ilter錛屾墽琛屾柟娉?/td>
 void init(FilterConfig filterConfig) 
           緇ф壙鑷狥ilter錛屽垵濮嬪寲鍙傛暟
protected  void postInit(Dispatcher dispatcher, FilterConfig filterConfig) 
          Callback for post initialization錛堜竴涓┖鐨勬柟娉曪紝鐢ㄤ簬鏂規硶鍥炶皟鍒濆鍖栵級


涓夈佹簮鐮佸墫鏋?nbsp;   


    1銆乮nit鏂規硶

         init鏄疐ilter絎竴涓繍琛岀殑鏂規硶錛屾垜浠湅涓媠truts2鐨勬牳蹇僃ilter鍦ㄨ皟鐢╥nit鏂規硶鍒濆鍖栨椂鍋氬摢浜涘伐浣滐細

 public void init(FilterConfig filterConfig) throws ServletException {
InitOperations init = new InitOperations();
try {
//灝佽filterConfig錛屽叾涓湁涓富瑕佹柟娉昰etInitParameterNames灝嗗弬鏁板悕瀛椾互String鏍煎紡瀛樺偍鍦↙ist涓?
FilterHostConfig config = new FilterHostConfig(filterConfig);
// 鍒濆鍖杝truts鍐呴儴鏃ュ織
init.initLogging(config);
//鍒涘緩dispatcher 錛屽茍鍒濆鍖栵紝榪欓儴鍒嗕笅闈㈡垜浠噸鐐瑰垎鏋愶紝鍒濆鍖栨椂鍔犺澆閭d簺璧勬簮
Dispatcher dispatcher = init.initDispatcher(config);
init.initStaticContentLoader(config, dispatcher);
//鍒濆鍖栫被灞炴э細prepare 銆乪xecute
prepare = new PrepareOperations(filterConfig.getServletContext(), dispatcher);
execute = new ExecuteOperations(filterConfig.getServletContext(), dispatcher);
this.excludedPatterns = init.buildExcludedPatternsList(dispatcher);
//鍥炶皟絀虹殑postInit鏂規硶
postInit(dispatcher, filterConfig);
} finally {
init.cleanup();
}
}


   棣栧厛鐪嬩笅FilterHostConfig 錛屾簮鐮佸涓嬶細


public class FilterHostConfig implements HostConfig {
private FilterConfig config;
/**
*鏋勯犲嚱鏁?
*/
public FilterHostConfig(FilterConfig config) {
this.config = config;
}
/**
*  鏍規嵁init-param閰嶇疆鐨刾aram-name鑾峰彇param-value鐨勫?
*/
public String getInitParameter(String key) {
return config.getInitParameter(key);
}
/**
*  榪斿洖鍒濆鍖栧弬鏁板悕鐨凩ist
*/
public Iterator<String> getInitParameterNames() {
return MakeIterator.convert(config.getInitParameterNames());
}
public ServletContext getServletContext() {
return config.getServletContext();
}
}

   鍙湁鐭煭鐨勫嚑琛屼唬鐮侊紝getInitParameterNames鏄繖涓被鐨勬牳蹇冿紝灝咶ilter鍒濆鍖栧弬鏁板悕縐版湁鏋氫婦綾誨瀷杞負Iterator銆傛綾葷殑涓昏浣滀負鏄filterConfig 灝佽銆?/p>



    閲嶇偣鏉ヤ簡錛屽垱寤哄茍鍒濆鍖朌ispatcher     

 public Dispatcher initDispatcher( HostConfig filterConfig ) {
Dispatcher dispatcher = createDispatcher(filterConfig);
dispatcher.init();
return dispatcher;
}

     鍒涘緩Dispatcher錛屼細璇誨彇 filterConfig 涓殑閰嶇疆淇℃伅錛屽皢閰嶇疆淇℃伅瑙f瀽鍑烘潵錛屽皝瑁呮垚涓轟竴涓狹ap錛岀劧鍚庢牴緇漵ervlet涓婁笅鏂囧拰鍙傛暟Map鏋勯燚ispatcher 錛?/p>

private Dispatcher createDispatcher( HostConfig filterConfig ) {
Map<String, String> params = new HashMap<String, String>();
for ( Iterator e = filterConfig.getInitParameterNames(); e.hasNext(); ) {
String name = (String) e.next();
String value = filterConfig.getInitParameter(name);
params.put(name, value);
}
return new Dispatcher(filterConfig.getServletContext(), params);
}

  Dispatcher鍒濆鍖栵紝鍔犺澆struts2鐨勭浉鍏抽厤緗枃浠訛紝灝嗘寜鐓ч『搴忛愪竴鍔犺澆錛歞efault.properties錛宻truts-default.xml,struts-plugin.xml,struts.xml錛?#8230;…


/**
*鍒濆鍖栬繃紼嬩腑渚濇鍔犺澆濡備笅閰嶇疆鏂囦歡
*/
public void init() {
if (configurationManager == null) {
configurationManager = new ConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME);
}
try {
//鍔犺澆org/apache/struts2/default.properties
init_DefaultProperties(); // [1]
//鍔犺澆struts-default.xml,struts-plugin.xml,struts.xml
init_TraditionalXmlConfigurations(); // [2]
init_LegacyStrutsProperties(); // [3]
//鐢ㄦ埛鑷繁瀹炵幇鐨凜onfigurationProviders綾?
init_CustomConfigurationProviders(); // [5]
//Filter鐨勫垵濮嬪寲鍙傛暟
init_FilterInitParameters() ; // [6]
init_AliasStandardObjects() ; // [7]
Container container = init_PreloadConfiguration();
container.inject(this);
init_CheckConfigurationReloading(container);
init_CheckWebLogicWorkaround(container);
if (!dispatcherListeners.isEmpty()) {
for (DispatcherListener l : dispatcherListeners) {
l.dispatcherInitialized(this);
}
}
} catch (Exception ex) {
if (LOG.isErrorEnabled())
LOG.error("Dispatcher initialization failed", ex);
throw new StrutsException(ex);
}
}


   鍒濆鍖杁efault.properties錛屽叿浣撶殑鍒濆鍖栨搷浣滃湪DefaultPropertiesProvider綾諱腑

  

 private void init_DefaultProperties() {
configurationManager.addConfigurationProvider(new DefaultPropertiesProvider());
}

    

   涓嬮潰鎴戜滑鐪嬩笅DefaultPropertiesProvider綾繪簮鐮侊細


public void register(ContainerBuilder builder, LocatableProperties props)
throws ConfigurationException {
Settings defaultSettings = null;
try {
defaultSettings = new PropertiesSettings("org/apache/struts2/default");
} catch (Exception e) {
throw new ConfigurationException("Could not find or error in org/apache/struts2/default.properties", e);
}
loadSettings(props, defaultSettings);
}


   鍏朵粬鐨勬垜浠啀嬈$渷鐣ワ紝澶у鍙互嫻忚涓嬪悇涓垵濮嬪寲鎿嶄綔閮藉姞杞戒簡閭d簺鏂囦歡


3銆乨oFilter鏂規硶

     doFilter鏄繃婊ゅ櫒鐨勬墽琛屾柟娉曪紝瀹冩嫤鎴彁浜ょ殑HttpServletRequest璇鋒眰錛孒ttpServletResponse鍝嶅簲錛屼綔涓簊trtus2鐨勬牳蹇冩嫤鎴櫒錛屽湪doFilter閲岄潰鍒板簳鍋氫簡鍝簺宸ヤ綔錛屾垜浠皢閫愯瑙h鍏舵簮鐮侊紝婧愮爜濡備笅錛?/p>


    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
//鐖剁被鍚戝瓙綾昏漿錛氬己杞負http璇鋒眰銆佸搷搴?
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
try {
//璁劇疆緙栫爜鍜屽浗闄呭寲
prepare.setEncodingAndLocale(request, response);
//鍒涘緩Action涓婁笅鏂囷紙閲嶇偣錛?
prepare.createActionContext(request, response);
prepare.assignDispatcherToThread();
if ( excludedPatterns != null && prepare.isUrlExcluded(request, excludedPatterns)) {
chain.doFilter(request, response);
} else {
request = prepare.wrapRequest(request);
ActionMapping mapping = prepare.findActionMapping(request, response, true);
if (mapping == null) {
boolean handled = execute.executeStaticResourceRequest(request, response);
if (!handled) {
chain.doFilter(request, response);
}
} else {
execute.executeAction(request, response, mapping);
}
}
} finally {
prepare.cleanupRequest(request);
}
}


    setEncodingAndLocale璋冪敤浜哾ispatcher鏂規硶鐨刾repare鏂規硶錛?/p>


/**
* Sets the request encoding and locale on the response
*/
public void setEncodingAndLocale(HttpServletRequest request, HttpServletResponse response) {
dispatcher.prepare(request, response);
}


   涓嬮潰鎴戜滑鐪嬩笅prepare鏂規硶錛岃繖涓柟娉曞緢綆鍗曞彧鏄緗簡encoding 銆乴ocale 錛屽仛鐨勫彧鏄竴浜涜緟鍔╃殑宸ヤ綔錛?/p>

public void prepare(HttpServletRequest request, HttpServletResponse response) {
String encoding = null;
if (defaultEncoding != null) {
encoding = defaultEncoding;
}
Locale locale = null;
if (defaultLocale != null) {
locale = LocalizedTextUtil.localeFromString(defaultLocale, request.getLocale());
}
if (encoding != null) {
try {
request.setCharacterEncoding(encoding);
} catch (Exception e) {
LOG.error("Error setting character encoding to '" + encoding + "' - ignoring.", e);
}
}
if (locale != null) {
response.setLocale(locale);
}
if (paramsWorkaroundEnabled) {
request.getParameter("foo"); // simply read any parameter (existing or not) to "prime" the request
}
}


   Action涓婁笅鏂囧垱寤猴紙閲嶇偣錛?/strong>

       ActionContext鏄竴涓鍣紝榪欎釜瀹規槗涓昏瀛樺偍request銆乻ession銆乤pplication銆乸arameters絳夌浉鍏充俊鎭?ActionContext鏄竴涓嚎紼嬬殑鏈湴鍙橀噺錛岃繖鎰忓懗鐫涓嶅悓鐨刟ction涔嬮棿涓嶄細鍏變韓ActionContext錛屾墍浠ヤ篃涓嶇敤鑰冭檻綰跨▼瀹夊叏闂銆傚叾瀹炶川鏄竴涓狹ap錛宬ey鏄爣紺簉equest銆乻ession銆?#8230;…鐨勫瓧絎︿覆錛屽兼槸鍏跺搴旂殑瀵硅薄錛?/p>

static ThreadLocal actionContext = new ThreadLocal();
Map<String, Object> context;

 
   涓嬮潰鎴戜滑鐪嬩笅濡備綍鍒涘緩action涓婁笅鏂囩殑錛屼唬鐮佸涓嬶細


/**
*鍒涘緩Action涓婁笅鏂囷紝鍒濆鍖杢hread local
*/
public ActionContext createActionContext(HttpServletRequest request, HttpServletResponse response) {
ActionContext ctx;
Integer counter = 1;
Integer oldCounter = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);
if (oldCounter != null) {
counter = oldCounter + 1;
}
//娉ㄦ剰姝ゅ鏄粠ThreadLocal涓幏鍙栨ActionContext鍙橀噺
ActionContext oldContext = ActionContext.getContext();
if (oldContext != null) {
// detected existing context, so we are probably in a forward
ctx = new ActionContext(new HashMap<String, Object>(oldContext.getContextMap()));
} else {
ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack();
stack.getContext().putAll(dispatcher.createContextMap(request, response, null, servletContext));
//stack.getContext()榪斿洖鐨勬槸涓涓狹ap<String錛孫bject>錛屾牴鎹Map鏋勯犱竴涓狝ctionContext
ctx = new ActionContext(stack.getContext());
}
request.setAttribute(CLEANUP_RECURSION_COUNTER, counter);
//灝咥ctionContext瀛樺ThreadLocal
ActionContext.setContext(ctx);
return ctx;
}


    涓婇潰浠g爜涓璬ispatcher.createContextMap錛屽浣曞皝瑁呯浉鍏沖弬鏁幫細


public Map<String,Object> createContextMap(HttpServletRequest request, HttpServletResponse response,
ActionMapping mapping, ServletContext context) {
// request map wrapping the http request objects
Map requestMap = new RequestMap(request);
// parameters map wrapping the http parameters.  ActionMapping parameters are now handled and applied separately
Map params = new HashMap(request.getParameterMap());
// session map wrapping the http session
Map session = new SessionMap(request);
// application map wrapping the ServletContext
Map application = new ApplicationMap(context);
//requestMap銆乸arams銆乻ession絳塎ap灝佽鎴愪負涓涓笂涓嬫枃Map錛岄愪釜璋冪敤浜唌ap.put(Map p).
Map<String,Object> extraContext = createContextMap(requestMap, params, session, application, request, response, context);
if (mapping != null) {
extraContext.put(ServletActionContext.ACTION_MAPPING, mapping);
}
return extraContext;
}


 鎴戜滑綆鍗曠湅涓婻equestMap錛屽叾浠栫殑鐪佺暐銆俁equestMap綾誨疄鐜頒簡鎶借薄Map錛屾晠鍏舵湰韜槸涓涓狹ap錛屼富瑕佹柟娉曞疄鐜幫細

//map鐨刧et瀹炵幇
public Object get(Object key) {
return request.getAttribute(key.toString());
}
//map鐨刾ut瀹炵幇
public Object put(Object key, Object value) {
Object oldValue = get(key);
entries = null;
request.setAttribute(key.toString(), value);
return oldValue;
}


   涓嬮潰鏄簮鐮佸睍紺轟簡濡備綍鎵цAction鎺у埗鍣細


public void executeAction(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ServletException {
dispatcher.serviceAction(request, response, servletContext, mapping);
}
public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,
ActionMapping mapping) throws ServletException {
//灝佽鎵ц鐨勪笂涓嬫枃鐜錛屼富瑕佽鐩稿叧淇℃伅瀛樺偍鍏ap
Map<String, Object> extraContext = createContextMap(request, response, mapping, context);
// If there was a previous value stack, then create a new copy and pass it in to be used by the new Action
ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
boolean nullStack = stack == null;
if (nullStack) {
ActionContext ctx = ActionContext.getContext();
if (ctx != null) {
stack = ctx.getValueStack();
}
}
if (stack != null) {
extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));
}
String timerKey = "Handling request from Dispatcher";
try {
UtilTimerStack.push(timerKey);
//鑾峰彇鍛藉悕絀洪棿
String namespace = mapping.getNamespace();
//鑾峰彇action閰嶇疆鐨刵ame灞炴?
String name = mapping.getName();
//鑾峰彇action閰嶇疆鐨刴ethod灞炴?
String method = mapping.getMethod();
Configuration config = configurationManager.getConfiguration();
//鏍規嵁鎵ц涓婁笅鏂囧弬鏁幫紝鍛藉悕絀洪棿錛屽悕縐扮瓑鍒涘緩鐢ㄦ埛鑷畾涔堿ction鐨勪唬鐞嗗璞?
ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
namespace, name, method, extraContext, true, false);
request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());
// if the ActionMapping says to go straight to a result, do it!
//鎵цexecute鏂規硶錛屽茍杞悜緇撴灉
if (mapping.getResult() != null) {
Result result = mapping.getResult();
result.execute(proxy.getInvocation());
} else {
proxy.execute();
}
// If there was a previous value stack then set it back onto the request
if (!nullStack) {
request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
}
} catch (ConfigurationException e) {
// WW-2874 Only log error if in devMode
if(devMode) {
String reqStr = request.getRequestURI();
if (request.getQueryString() != null) {
reqStr = reqStr + "?" + request.getQueryString();
}
LOG.error("Could not find action or result\n" + reqStr, e);
}
else {
LOG.warn("Could not find action or result", e);
}
sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e);
} catch (Exception e) {
sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
} finally {
UtilTimerStack.pop(timerKey);
}
}


   鏂囦腑瀵瑰浣曡В鏋怱truts.xml錛屽浣曞皢URL涓巃ction鏄犲皠鍖歸厤涓哄垎鏋愶紝鏈夐渶瑕佺殑鎴戝悗緇ˉ鍏紝鍥犱負StrutsXmlConfigurationProvider緇ф壙XmlConfigurationProvider錛屽茍鍦╮egister鏂規硶鍥炶皟鐖剁被鐨剅egister錛屾湁鍏磋叮鐨勫彲浠ユ繁鍏ラ槄璇諱笅涓媂mlConfigurationProvider婧愮爜錛?/p>


 public void register(ContainerBuilder containerBuilder, LocatableProperties props) throws ConfigurationException {
if (servletContext != null && !containerBuilder.contains(ServletContext.class)) {
containerBuilder.factory(ServletContext.class, new Factory<ServletContext>() {
public ServletContext create(Context context) throws Exception {
return servletContext;
}
});
}
//璋冪敤鐖剁被鐨剅egister錛屽叧閿偣鎵鍦?
super.register(containerBuilder, props);
}



     struts2-core-2.2.1.jar鍖呬腑struts-2.1.7.dtd瀵逛簬Action鐨勫畾涔夊涓嬶細

<!ELEMENT action (param|result|interceptor-ref|exception-mapping)*>
<!ATTLIST action
name CDATA #REQUIRED
class CDATA #IMPLIED
method CDATA #IMPLIED
converter CDATA #IMPLIED
>

    浠庝笂榪癉TD涓彲瑙丄ction鍏冪礌鍙互鍚湁name 銆乧lass 銆乵ethod 銆乧onverter 灞炴с?/p>


   XmlConfigurationProvider瑙f瀽struts.xml閰嶇疆鐨凙ction鍏冪礌錛?/p>

   protected void addAction(Element actionElement, PackageConfig.Builder packageContext) throws ConfigurationException {
String name = actionElement.getAttribute("name");
String className = actionElement.getAttribute("class");
String methodName = actionElement.getAttribute("method");
Location location = DomHelper.getLocationObject(actionElement);
if (location == null) {
LOG.warn("location null for " + className);
}
//methodName should be null if it's not set
methodName = (methodName.trim().length() > 0) ? methodName.trim() : null;
// if there isnt a class name specified for an <action/> then try to
// use the default-class-ref from the <package/>
if (StringUtils.isEmpty(className)) {
// if there is a package default-class-ref use that, otherwise use action support
/* if (StringUtils.isNotEmpty(packageContext.getDefaultClassRef())) {
className = packageContext.getDefaultClassRef();
} else {
className = ActionSupport.class.getName();
}*/
} else {
if (!verifyAction(className, name, location)) {
if (LOG.isErrorEnabled())
LOG.error("Unable to verify action [#0] with class [#1], from [#2]", name, className, location.toString());
return;
}
}
Map<String, ResultConfig> results;
try {
results = buildResults(actionElement, packageContext);
} catch (ConfigurationException e) {
throw new ConfigurationException("Error building results for action " + name + " in namespace " + packageContext.getNamespace(), e, actionElement);
}
List<InterceptorMapping> interceptorList = buildInterceptorList(actionElement, packageContext);
List<ExceptionMappingConfig> exceptionMappings = buildExceptionMappings(actionElement, packageContext);
ActionConfig actionConfig = new ActionConfig.Builder(packageContext.getName(), name, className)
.methodName(methodName)
.addResultConfigs(results)
.addInterceptors(interceptorList)
.addExceptionMappings(exceptionMappings)
.addParams(XmlHelper.getParams(actionElement))
.location(location)
.build();
packageContext.addActionConfig(name, actionConfig);
if (LOG.isDebugEnabled()) {
LOG.debug("Loaded " + (StringUtils.isNotEmpty(packageContext.getNamespace()) ? (packageContext.getNamespace() + "/") : "") + name + " in '" + packageContext.getName() + "' package:" + actionConfig);
}
}



     宸ヤ綔涓笉娑夊強Struts2錛屾湰鍛ㄥ伐浣滄湁涓?澶╃殑絀烘。鏈燂紝紼嶅井鐪嬩簡涓媠truts2鐨勬枃妗o紝鍐欎簡涓猟emo錛屼粠婧愮爜鐨勮搴︾爺絀朵簡涓嬭繍琛屽師鐞嗭紝濡傛湁鍒嗘瀽涓嶅綋璇鋒寚鍑猴紝鎴戝悗緇愭瀹屽杽鏇存錛屽ぇ瀹跺叡鍚屾彁楂樸?/p>




絀虹櫧 2011-05-10 20:58 鍙戣〃璇勮
]]>
Nginx+tomcat閰嶇疆闆嗙兢璐熻澆鍧囪 http://www.tkk7.com/niumd/archive/2011/05/10/349958.html絀虹櫧絀虹櫧Tue, 10 May 2011 12:56:00 GMThttp://www.tkk7.com/niumd/archive/2011/05/10/349958.htmlhttp://www.tkk7.com/niumd/comments/349958.htmlhttp://www.tkk7.com/niumd/archive/2011/05/10/349958.html#Feedback5http://www.tkk7.com/niumd/comments/commentRss/349958.htmlhttp://www.tkk7.com/niumd/services/trackbacks/349958.html

  浣滆咃細niumd 

  Blog:http://ari.iteye.com

  杞澆璇鋒敞鏄庡嚭澶勶紝璋㈣阿


    寮鍙戠殑搴旂敤閲囩敤F5璐熻澆鍧囪 浜ゆ崲鏈猴紝F5灝嗚姹傝漿鍙戠粰5鍙癶p unix鏈嶅姟鍣紝姣忓彴鏈嶅姟鍣ㄦ湁澶氫釜webserver瀹炰緥錛屽澶栨彁渚泈eb鏈嶅姟鍜宻ocket絳夋帴鍙f湇鍔°備箣鍒濓紝鏇炬湁涓皬灝忕殑鐤戦棶涓轟綍涓嶉噰鐢ㄥ紑婧愮殑apache銆丯ginx杞歡璐熻澆錛孎5璁懼鍔ㄨ緞鍑犲崄涓囷紝浠鋒牸鏄傝吹錛熻嚜宸變竴涓瘮杈冨辜紼氱殑闂錛屽悗緇槑鐧斤細F5鏄搷浣滀簬IOS緗戠粶妯″瀷鐨勪紶杈撳眰錛孨ginx銆乤pache鏄熀浜巋ttp鍙嶅悜浠g悊鏂瑰紡錛屼綅浜嶪SO妯″瀷鐨勭涓冨眰搴旂敤灞傘傜洿鐧戒簺灝辨槸TCP UDP 鍜宧ttp鍗忚鐨勫尯鍒紝Nginx涓嶈兘涓哄熀浜嶵CP鍗忚鐨勫簲鐢ㄦ彁渚涜礋杞藉潎琛°?/p>


      浜嗚В浜嗕簩鑰呬箣闂寸殑鍖哄埆浜庡簲鐢ㄥ満鏅紝瀵筃ginx浜х敓嫻撳帤鐨勫叴瓚o紝闃呰寮犲鐨?lt;瀹炴垬Nginx>錛堣繖涓?5騫寸殑灝忎紮瀛愬勾杞繪湁涓虹盡鎱?濡掑繉錛夛紝鎼炴槑鐧戒簡澶ц嚧鍘熺悊鍜岄厤緗紝Ubuntu10.10錛寃indow涓嬪Nginx+tomcat璐熻澆鍧囪 鍋氫簡閰嶇疆灝濊瘯錛屽皢鍏ㄩ儴璇鋒眰杞彂鍒皌omcat錛屽茍鏈仛闈欐侊紝鍔ㄦ佸垎寮錛屽浘鐗囬槻鐩楅摼絳夐厤緗?br /> Nginx 浠嬬粛


     Nginx 錛堝彂闊沖悓 engine x錛夋槸涓嬈捐交閲忕駭鐨刉eb 鏈嶅姟鍣紡鍙嶅悜浠g悊鏈嶅姟鍣ㄥ強鐢靛瓙閭歡錛圛MAP/POP3錛変唬鐞嗘湇鍔″櫒錛屽茍鍦ㄤ竴涓狟SD-like 鍗忚涓嬪彂琛屻?nbsp; 鍏剁壒鐐規槸鍗犳湁鍐呭瓨灝戯紝騫跺彂鑳藉姏寮猴紝浜嬪疄涓妌ginx鐨勫茍鍙戣兘鍔涚‘瀹炲湪鍚岀被鍨嬬殑緗戦〉浼烘湇鍣ㄤ腑琛ㄧ幇杈冨ソ.鐩墠涓浗澶ч檰浣跨敤nginx緗戠珯鐢ㄦ埛鏈夛細鏂版氮銆佺綉鏄撱?鑵捐,鍙﹀鐭ュ悕鐨勫井緗戝織Plurk涔熶嬌鐢╪ginx銆?/p>


    涓婇潰鐨勫叏鏄疦ginx浠嬬粛鍩烘湰涓婃槸搴熻瘽錛屼笅闈㈣漿鍏ユ棰橈紝鍥炬枃緇撳悎灞曠ず鍩烘湰閰嶇疆錛岄鍏堟槸window鐜銆佸叾嬈℃槸Ubuntu鐜錛圴box铏氭嫙錛夈傛湰鏂囦富瑕佸熀浜嶯ginx涓嬮厤緗袱鍙皌omcat錛岀粨鏋勫涓嬪浘錛?br />
 

Window xp鐜錛歂ginx+Tomcat6

1銆佷笅杞藉湴鍧

       http://nginx.org/en/download.html 錛岃繖閲屾垜浠帹鑽愪笅杞界ǔ瀹氱増錛坰table versions錛夛紝鏈枃閲囩敤nginx-0.8.20銆?/p>


2銆佺洰褰曠粨鏋?/strong>


      Nginx-

               |_  conf   閰嶇疆鐩綍

               |_  contrib

               |_  docs 鏂囨。鐩綍

               |_  logs  鏃ュ織鐩綍

               |_  temp 涓存椂鏂囦歡鐩綍

               |_  html 闈欐侀〉闈㈢洰褰?/p>

               |_  nginx.exe 涓葷▼搴?/p>


      window涓嬪畨瑁匩ginx鏋佸叾綆鍗曪紝瑙e帇緙╁埌涓涓棤絀烘牸鐨勮嫳鏂囩洰褰曞嵆鍙紙涓漢涔犳儻錛屾媴蹇冧腑鏂囧嚭闂錛夛紝鍙屽嚮nginx鍚姩錛岃繖閲屾垜瀹夎鍒幫細D:\server鐩綍錛屼笅闈㈡秹鍙婂埌鐨則omcat涔熷畨瑁呭湪姝ょ洰褰曘?/p>

   

DOS鐜鍚姩

 

鑻ユ灉鎯沖仠姝ginx錛宒os鐜榪愯鍛戒護錛歯ginx -s stop


3銆乶ginx.conf閰嶇疆


   Nginx閰嶇疆鏂囦歡榛樿鍦╟onf鐩綍錛屼富瑕侀厤緗枃浠朵負nginx.conf錛屾垜浠畨瑁呭湪D:\server\nginx-0.8.20銆侀粯璁や富閰嶇疆鏂囦歡涓篋:\server\nginx-0.8.20\nginx.conf銆備笅闈㈡槸nginx浣滀負鍓嶇鍙嶅悜浠g悊鏈嶅姟鍣ㄧ殑閰嶇疆銆?/p>

#Nginx鎵鐢ㄧ敤鎴峰拰緇勶紝window涓嬩笉鎸囧畾
#user  niumd niumd;
#宸ヤ綔鐨勫瓙榪涚▼鏁伴噺錛堥氬父絳変簬CPU鏁伴噺鎴栬?鍊嶄簬CPU錛?
worker_processes  2;
#閿欒鏃ュ織瀛樻斁璺緞
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;
#鎸囧畾pid瀛樻斁鏂囦歡
pid        logs/nginx.pid;
events {
#浣跨敤緗戠粶IO妯″瀷linux寤鴻epoll錛孎reeBSD寤鴻閲囩敤kqueue錛寃indow涓嬩笉鎸囧畾銆?
#use epoll;
#鍏佽鏈澶ц繛鎺ユ暟
worker_connections  2048;
}
http {
include       mime.types;
default_type  application/octet-stream;
#瀹氫箟鏃ュ織鏍煎紡
#log_format  main  '$remote_addr - $remote_user [$time_local] $request '
#                  '"$status" $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';
#access_log  off;
access_log  logs/access.log;
client_header_timeout  3m;
client_body_timeout    3m;
send_timeout           3m;
client_header_buffer_size    1k;
large_client_header_buffers  4 4k;
sendfile        on;
tcp_nopush      on;
tcp_nodelay     on;
#keepalive_timeout  75 20;
include    gzip.conf;
upstream localhost {
#鏍規嵁ip璁$畻灝嗚姹傚垎閰嶅悇閭d釜鍚庣tomcat錛岃澶氫漢璇涓哄彲浠ヨВ鍐硈ession闂錛屽叾瀹炲茍涓嶈兘銆?
#鍚屼竴鏈哄櫒鍦ㄥ緗戞儏鍐典笅錛岃礬鐢卞垏鎹紝ip鍙兘涓嶅悓
#ip_hash;
server localhost:18081;
server localhost:18080;
}
server {
listen       80;
server_name  localhost;
location / {
proxy_connect_timeout   3;
proxy_send_timeout      30;
proxy_read_timeout      30;
proxy_pass http://localhost;
}
}
}

 
   浠g悊璁劇疆濡備笅錛?/p>

proxy_redirect          off;
proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   300;
proxy_send_timeout      300;
proxy_read_timeout      300;
proxy_buffer_size       4k;
proxy_buffers           4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;


   gzip鍘嬬緝鐩稿叧閰嶇疆濡備笅錛?/p>

gzip              on;
gzip_min_length      1000;
gzip_types         text/plain text/css application/x-javascript;

 
  4銆乀omcat閰嶇疆


   瀵逛簬tomcat澶у閮藉緢鐔熸倝錛屽彧闇瑕佷慨鏀箂erver.xml閰嶇疆鏂囦歡鍗沖彲錛岃繖閲屾垜浠互apache-tomcat-6.0.14涓轟緥錛屽垎鍒湪server鐩綍錛岃В鍘嬬緝騫跺懡鍚嶄負錛歛pache-tomcat-6.0.14_1銆乤pache-tomcat-6.0.14_2銆?/p>


    絎竴澶勭鍙d慨鏀癸細

<!--  淇敼port绔彛錛?8006 淇╀釜tomcat涓嶈兘閲嶅錛岀鍙i殢鎰忥紝鍒お灝?->
<Server port="18006" shutdown="SHUTDOWN">

 
   絎簩澶勭鍙d慨鏀癸細

<!-- port="18081" tomcat鐩戝惉绔彛錛岄殢鎰忚緗紝鍒お灝?-->
<Connector port="18081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
 


   絎笁澶勭鍙d慨鏀癸細

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


   Engine鍏冪礌澧炲姞jvmRoute灞炴э細

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

 
    涓や釜tomcat鐨勭鍙e埆閲嶅錛屼繚璇佽兘鍚姩璧鋒潵錛屽彟涓涓猼omcat閰嶇疆甯屾嵎鐪佺暐錛岀洃鍚鍙d負18080錛岄檮浠朵腑鎴戜滑灝嗕笂浼犳墍鏈夌殑閰嶇疆淇℃伅銆?/p>


5銆侀獙璇侀厤緗笌嫻嬭瘯璐熻澆鍧囪 


    棣栧厛嫻嬭瘯nginx閰嶇疆鏄惁姝g‘錛屾祴璇曞懡浠わ細nginx -t  (榛樿楠岃瘉:conf\nginx.conf),涔熷彲浠ユ寚瀹氶厤緗枃浠惰礬寰勩?/p>

 姝や緥nginx瀹夎鐩綍錛欴:\server\nginx-0.8.20錛宒os鐜涓嬪浘鐢婚潰鎴愬姛紺轟緥錛?br />

   鍏舵楠岃瘉tomcat錛屽惎鍔ㄤ袱涓猼omcat錛屼笉鍑虹幇绔彛鍐茬獊鍗充負鎴愬姛錛坱omcat渚濊禆鐨刯ava絳夋悶“鎸ㄨ涪”鐨勫氨搴熻瘽涓嶈浜嗭級錛?/p>

 

    鏈鍚庨獙璇侀厤緗礋杞藉潎琛¤緗紝http://localhost/ 鎴?a href="http://localhost/index.jsp" mce_href="http://localhost/index.jsp">http://localhost/index.jsp 銆傛垜淇敼浜唅ndex.jsp欏甸潰錛屽鍔犳棩蹇楄緭鍑轟俊鎭紝渚夸簬瑙傚療銆傛敞鎰忥細宸︿笂瑙掑皬鐚ご涓婄殑錛歛ccess tomcat2銆乤ccess tomcat1銆傝鏄庤闂簡涓嶅悓鐨則omcat銆?/p>

  

 
     鑷蟲window涓媙ginx+tomcat璐熻澆鍧囪 閰嶇疆緇撴潫錛屽叧浜巘omcat Session鐨勯棶棰橀氬父鏄噰鐢╩emcached錛屾垨鑰呴噰鐢╪ginx_upstream_jvm_route 錛屼粬鏄竴涓?Nginx 鐨勬墿灞曟ā鍧楋紝鐢ㄦ潵瀹炵幇鍩轟簬 Cookie 鐨?Session Sticky 鐨勫姛鑳姐傚鏋渢omcat榪囧涓嶅緩璁畇ession鍚屾錛宻erver闂寸浉浜掑悓姝ession寰堣楄祫婧愶紝楂樺茍鍙戠幆澧冨鏄撳紩璧稴ession椋庢毚銆傝鏍規嵁鑷繁搴旂敤鎯呭喌鍚堢悊閲囩撼session瑙e喅鏂規銆?/p>



 浣滆咃細niumd 

  Blog:http://ari.iteye.com



Ubuntu10.10鐜錛歂ginx+Tomcat6


鎴戜滑涓嬮潰綆鍗曡涓媢buntu10.10涓嬪浣曞畨瑁呴厤緗紝涓昏浠ュ浘鐗囦負涓伙紝綆鍗曡В閲娿?/p>

1銆佷笅杞絅ginx

      鍦板潃錛?a mce_>http://nginx.org/en/download.html 錛宭inux鐗堟湰錛歯ginx-0.8.20.tar.銆傝В鍘嬬緝鍛戒護錛?/p>


tar -zxvf nginx-0.8.20.tar.gz


2銆佺紪璇戝畨瑁匩ginx


     Nginx渚濊禆涓浜涘叾浠朠CRE銆乷penssl錛堜緷璧杔ibssl-dev錛夛紝鏈漢絎旇鏈琔buntu鐜宸茬粡瀹夎PCRE錛屼粎闇瀹夎渚濊禆鐨刼penssl錛屼笅闈㈡垜浠畝鍗曡涓嬪浣曞畨瑁匬CRE鍜宱penssl絳?/p>


     PCRE涓嬭澆鍦板潃錛?a href="ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/" mce_href="ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/">ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/


tar zxvf  pcre-8.01.tar.gz
cd pcre-8.01
sudo ./configure
sodu make
sodu make install


    openssl閫氳繃apt-get install瀹夎錛屽懡浠ゃ佹埅鍥懼涓嬶細

sudo apt-get install openssl
sudo apt-get install libssl-dev
//濡傜己灝戝叾浠栧寘錛岃閲囩敤姝ゆ柟娉曞畨瑁咃紝ubuntu鏈変緷璧栨彁紺?

 

 
 渚濊禆鐨勮蔣浠跺寘瀹夎瀹屾瘯錛屼笅闈㈡潵緙栬瘧Nginx錛?/p>

#灝唚indow鍏變韓鐩綍杞歡鎷瘋礉鍒板綋鍓嶅伐浣滅洰褰?
cp /mnt/fileshare/nginx-0.8.20.tar.gz ./
#瑙e帇緙╄蔣浠跺寘
tar zxvf nginx-0.8.20.tar.gz
cd nginx-0.8.20
//緙栬瘧婧愮爜,榛樿浣跨敤nobody錛屾寚瀹氭湰鏈哄凡瀛樺湪鐨勭敤鎴鳳紝緇勶紝鍚敤nginx-status鍔熻兘錛岀洃鎺ginx鐘舵併傚惎鍔╠ebug
sudo ./configure  --user=niumd --group=niumd --with-debug --with-http_stub_status_module
sudo make
sudo make install

 
   鎴浘 濡備笅錛?br />

瀹夎緇撴灉鎴浘濡備笅錛?/p>

 

緙栬瘧瀹夎姝g‘緇撴潫錛屾寜鐓т笂榪皐indow涓嬫柟娉曟鏌ラ粯璁ら厤緗紝鐒跺悗鍦ㄩ粯璁ら厤緗笅鍚姩nginx錛岃闂?a mce_>http://127.0.0.1 錛屽涓嬪浘璇存槑鎴愬姛

 

Nginx閰嶇疆鎴愬姛鍚庢垜浠window涓媙ginx.conf灝戝仛淇敼錛屽涓嬶細

#Nginx鎵鐢ㄧ敤鎴峰拰緇?
user  niumd niumd;
#宸ヤ綔鐨勫瓙榪涚▼鏁伴噺錛堥氬父絳変簬CPU鏁伴噺鎴栬?鍊嶄簬CPU錛?
worker_processes  2;
#閿欒鏃ュ織瀛樻斁璺緞
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;
#鎸囧畾pid瀛樻斁鏂囦歡
pid        logs/nginx.pid;
events {
#浣跨敤緗戠粶IO妯″瀷linux寤鴻epoll錛孎reeBSD寤鴻閲囩敤kqueue
use epoll;
#鍏佽鏈澶ц繛鎺ユ暟
worker_connections  2048;
}
http {
include       mime.types;
default_type  application/octet-stream;
#瀹氫箟鏃ュ織鏍煎紡
#log_format  main  '$remote_addr - $remote_user [$time_local] $request '
#                  '"$status" $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';
#access_log  off;
access_log  logs/access.log;
client_header_timeout  3m;
client_body_timeout    3m;
send_timeout           3m;
client_header_buffer_size    1k;
large_client_header_buffers  4 4k;
sendfile        on;
tcp_nopush      on;
tcp_nodelay     on;
#keepalive_timeout  75 20;
include    gzip.conf;
upstream localhost {
#ip_hash
#ip_hash;
server localhost:18081;
server localhost:18080;
}
server {
listen       80;
server_name  localhost;
location / {
proxy_connect_timeout   3;
proxy_send_timeout      30;
proxy_read_timeout      30;
proxy_pass http://localhost;
}
}
}

  瀵逛簬涓婇潰鍏充簬ubuntu涓婲ginx閰嶇疆鍜寃indow涓嬪熀鏈浉鍚岋紝鍖哄埆鍦ㄤ嬌鐢ㄧ殑IO緗戠粶妯″瀷錛宭inux涓嬪緩璁嬌鐢╡poll錛屽彟澶栧氨鏄繍琛屾墍鐢ㄧ殑鐢ㄦ埛鍜岀粍錛?/p>


3銆侀厤緗畉omcat

    璇峰弬鑰僿indow涓嬮厤緗紝瀹屽叏鐩稿悓銆?/p>

4銆佸惎鍔ㄥ仠姝ginx

    ubuntu涓嬪惎鍔╪ginx涓巜indow紼嶆湁涓嶅悓錛屽ぇ鑷村惎鍔ㄥ仠姝㈡柟娉曞涓嬨?/p>


#nginx鐩綍鎵ц
sbin/nginx
鎴栭氳繃-c 鎸囧畾閰嶇疆鏂囦歡
sbin/nginx -c usr/local/nginx8.20/conf/nginx/conf



niumd@niumd-laptop:/usr/local/nginx$ pwd
/usr/local/nginx
niumd@niumd-laptop:/usr/local/nginx$ sudo sbin/nginx -t
the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful
niumd@niumd-laptop:/usr/local/nginx$ sudo sbin/nginx -v
nginx version: nginx/0.8.20
niumd@niumd-laptop:/usr/local/nginx$ sudo sbin/nginx -V
nginx version: nginx/0.8.20
built by gcc 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
configure arguments: --user=niumd --group=niumd --with-debug --with-http_sub_module
niumd@niumd-laptop:/usr/local/nginx$ sudo sbin/nginx
niumd@niumd-laptop:/usr/local/nginx$ ps -ef|grep nginx
root      5158     1  0 22:32 ?        00:00:00 nginx: master process sbin/nginx
niumd     5159  5158  0 22:32 ?        00:00:00 nginx: worker process
niumd     5161  1577  0 22:32 pts/0    00:00:00 grep --color=auto nginx
niumd@niumd-laptop:/usr/local/nginx$ 


     鎴戜滑閫氳繃ps  -ef|grep nginx,鐪嬪埌濡備笅緇撴灉:

   

娉ㄦ剰錛氬湪鍚姩鏃秎inux鎻愮ず涓鍙ヨ鍛娿恮arn銆?#8230;…錛屾槸鍥犱負鎴戜滑璁劇疆鐨?#鍏佽鏈澶ц繛鎺ユ暟 worker_connections  2048錛岃秴榪噇inux榛樿1024鐨勯檺鍒躲?/p>

       鍋滄錛歬ill -淇″彿綾誨瀷 pid

       nginx/logs鐩綍涓嬫湁涓猲ginx銆俻id鐨勬枃浠訛紝姝ゆ枃浠惰褰曚簡姣忔榪愯鐨刾id錛屼篃鍙互閫氳繃ps鍛戒護鏌ヨ銆?/p>

淇″彿綾誨瀷濡備笅錛?/p>


淇″彿鎺у埗
淇″彿綾誨瀷 鎻忚堪
RERM.INT 蹇熷叧闂?/td>
HUP 騫蟲粦閲嶅惎錛屽姞杞介厤緗?/td>
USR1 閲嶆柊鍔犺澆鏃ュ織
USER2 騫蟲粦鍗囩駭鎵ц紼嬪簭
WINCH 浠庡鍏抽棴宸ヤ綔榪涚▼
QUIT 浠庡鍏抽棴


鍙傝冭祫鏂欙細

http://www.oschina.net/bbs/thread/9301

oschina.net 鐢熶駭閰嶇疆錛屾緗戠珯閲囩敤java璇█錛宯ginx錛宼omcat鏈嶅姟鍣ㄣ?/p>

http://nginx.org/

寮犲錛?lt;<瀹炴垬Nginx>>




絀虹櫧 2011-05-10 20:56 鍙戣〃璇勮
]]>
Ubuntu11 Unity 2D妗岄潰http://www.tkk7.com/niumd/archive/2011/05/10/349955.html絀虹櫧絀虹櫧Tue, 10 May 2011 12:54:00 GMThttp://www.tkk7.com/niumd/archive/2011/05/10/349955.htmlhttp://www.tkk7.com/niumd/comments/349955.htmlhttp://www.tkk7.com/niumd/archive/2011/05/10/349955.html#Feedback0http://www.tkk7.com/niumd/comments/commentRss/349955.htmlhttp://www.tkk7.com/niumd/services/trackbacks/349955.html
Ubuntu11.04濡備綍璁劇疆2D Unity妗岄潰


    Ubuntu11.04涓鍙戝竷灝辮揩涓嶅強寰呯殑灝濋矞錛岃櫄鎷熸満鐜涓嶆敮鎸乁nity 3D,灝濊瘯浜嗕笅Unity 2D錛屾晥鏋滃緢鐐傚畨瑁呭緢綆鍗曪紝緇欏ぇ瀹跺垎浜笅銆?/p>

Terminal 閿叆濡備笅鍛戒護錛?/span>


sudo add-apt-repository ppa:unity-2d-team/unity-2d-daily
sudo apt-get update
sudo apt-get install unity-2d
 

   榪欎笁琛屽懡浠ゅ氨涓嶈В閲婏紝Ubuntu鏂版墜鑰佹墜閮藉簲璇ユ竻妤氾紝緇忚繃10-20鍒嗛挓鐨勭瓑寰咃紙鍙栧喅浣犵殑緗戦熷拰閫夋嫨鏇存柊婧愶級錛孶buntu瀹夎Unity-2d鍜屼緷璧栧寘錛屽畨瑁呭畬姣曪紝log out錛岃繖鐐規瘮windows寮猴紝xp閮芥槸鎻愮ず閲嶅惎鐨勩?/p>


 涓婂浘錛氱櫥闄嗙晫闈㈤夋嫨Unity 2D銆?/p>


 涓婂浘縐妗岄潰錛?/span>





絀虹櫧 2011-05-10 20:54 鍙戣〃璇勮
]]>
crontab鍛戒護鐢ㄦ硶http://www.tkk7.com/niumd/archive/2011/05/10/349957.html絀虹櫧絀虹櫧Tue, 10 May 2011 12:54:00 GMThttp://www.tkk7.com/niumd/archive/2011/05/10/349957.htmlhttp://www.tkk7.com/niumd/comments/349957.htmlhttp://www.tkk7.com/niumd/archive/2011/05/10/349957.html#Feedback0http://www.tkk7.com/niumd/comments/commentRss/349957.htmlhttp://www.tkk7.com/niumd/services/trackbacks/349957.html

   Linux鎻愪緵浜嗙敤鎴鋒帶鍒朵緥琛屼換鍔$殑鍛戒護crontab錛屽父鐢ㄤ簬姣忛棿闅斾竴瀹氭椂闂村驚鐜墽琛屼竴浜涜剼鏈紝姝ゅ鎴戜滑鏆傛椂縐頒負錛歀inux瀹氭椂浠誨姟銆?/p>


#闂笅鐢蜂漢crontab鐨勭敤娉?
man crontab
crontab [ -u user ] { -l | -r [ -i ] | -e }
鍙傛暟錛?
-u錛氬彧鏈塺oot鎵嶅彲浠ユ墽琛屾浠誨姟
-l 錛氭煡鐪媍rontab宸ヤ綔鍐呭
-e錛氱紪杈慶rontab宸ヤ綔鍐呭
-r 錛氬垹闄rontab宸ヤ綔鍐呭



   crontab搴旂敤鍦烘櫙涓句緥錛氬畾鏃墮噰闆嗚繙紼嬫湇鍔″櫒鏂囦歡

   鎵嬫満鍙戦佺煭淇★紝鐭俊鍥炰互鏂囨湰褰㈠紡璁板綍鍦ㄤ氦鎹㈡満涓婂艦鎴愮煭淇¤瘽鍗曪紝鐭俊璇濆崟閫氬父鏄揪鍒?M錛屽鏋滀笉婊?m鍒欐瘡浜斿垎閽熷艦鎴愪竴涓瘽鍗曪紙涓嶅悓鐨勪氦鎹㈡満鍙兘瀛樺湪宸紓錛夈傚鏋滃璇濆崟璁¤垂錛屽綋鐒墮渶瑕佸鐭俊璇濆崟榪涜閲囬泦錛岀劧鍚庤繘琛屽悗緇璐瑰伐浣溿傛垜浠亣璁鵑噰鐢╯hell鎴栬卲ython鑴氭湰閲囬泦錛屾殏涓旂敤shell涓句緥錛屽亣璁鵑噰闆唖hell涓篴cquisition.sh 錛屾瘡闂撮殧1鍒嗛挓閲囬泦涓嬈°?/p>


    鍛戒護緇堢鎵ц錛?/p>


#鏍囩ず緙栬緫渚嬭浠誨姟
crontab -e

    鐒跺悗鎴戜滑灝嗕細鐪嬪埌濡備笅鐢婚潰錛?/p>

 

 


   宸茬粡榪涘叆VI 緙栬緫妯″紡錛屽湪閲岄潰杈撳叆涓嬮潰榪欒錛屾寜涓婨SC->錛?>wq灝變繚瀛樹簡

#姣忛棿闅斾竴鍒嗛挓鎵ц涓嬈¢噰闆嗚剼鏈?
*/1 * * * * crontab  /璺緞/acquisition.sh



    crontab鐨勬牸寮忎負錛?/p>

    鍒嗛挓  灝忔椂 鏃?nbsp; 鏈?鍛?crontab   寰呮墽琛屽懡浠ゆ垨鑰呰剼鏈?nbsp;


    鍏朵腑錛? 浠h〃浠諱綍鏃墮棿閮芥帴鍙楋紝濡備笂渚嬪皬鏃躲佹棩鏈熴佹湀浠姐佸懆閮戒負*

             錛屼唬琛ㄥ垎鍓叉椂闂存錛屽鍒嗛挓淇敼涓猴細0,1,2,3,4,5 鍗充換浣曞皬鏃跺湴1,2錛?#8230;…6鍒嗛挓閮芥墽琛?/p>

             - 浠h〃鏃墮棿孌典笂闈㈢殑姣忛棿闅斾竴鍒嗛挓鍙互鏍囩ず涓?-59錛?/p>

             /n 浠h〃姣忛棿闅旓紝鍒嗛挓浣嶇疆錛?/5鏍囩ず姣忛棿闅斾簲鍒嗛挓


 娉ㄦ剰錛?鍜?涔嬮棿鍙湁涓涓┖鏍鹼紱



EOF



絀虹櫧 2011-05-10 20:54 鍙戣〃璇勮
]]>
嫻呰皥Selector鍒涘緩鏈哄埗http://www.tkk7.com/niumd/archive/2011/05/10/349954.html絀虹櫧絀虹櫧Tue, 10 May 2011 12:53:00 GMThttp://www.tkk7.com/niumd/archive/2011/05/10/349954.htmlhttp://www.tkk7.com/niumd/comments/349954.htmlhttp://www.tkk7.com/niumd/archive/2011/05/10/349954.html#Feedback0http://www.tkk7.com/niumd/comments/commentRss/349954.htmlhttp://www.tkk7.com/niumd/services/trackbacks/349954.html

鍓嶆鏃墮棿闃呰mina婧愮爜鏃訛紝鐞?/span>瑙?span mce_style="font-family: monospace; white-space: pre;" style="font-family: monospace; white-space: pre; ">Selector瀹炰緥鍖栨満鍒剁粏鑺傛湁鐐圭枒鎯戠枒鎯戯紝涓昏鏄?/span>SelectorProvider鐨勭粏鑺傚疄鐜版柟闈€?/span>

閫氬父鍒涘緩涓涓猄elector錛岄氳繃闈欐乷pen鏂規硶鍒涘緩涓涓疄渚嬨?/span>浠g爜濡備笅錛?/p>

Selector selector = Selector.open();


瑙傚療JDK婧愮爜鍙戠幇Select鐨勫垱寤洪氳繃SelectorProvider杈呭姪綾繪潵瀹屾垚錛?/p>


public static Selector open() throws IOException {
return SelectorProvider.provider().openSelector();
}



榪涗竴姝ヨ瀵?span mce_style="white-space: pre;" style="white-space: pre; ">SelectorProvider綾籶rovider鏂規硶婧愮爜錛屽紩鐢ㄥ埌綾籹un.nio.ch.DefaultSelectorProvider錛屽紑濮嬬殑鏃跺欑敱浜庡湪JDK API

涓病鎵懼埌璇ョ被錛屾簮鐮侀噷闈篃娌℃壘鍒幫紝姣旇緝鐤戞儜濡備綍鍒涘緩鐨勩?/span>浠婂ぉ鍦╮t.jar鎵懼埌浜嗚綾伙紝騫舵壘鍒板叾瀵規簮鐮併?/p>


/**
* Returns the default SelectorProvider.
*/
public static SelectorProvider create() {
PrivilegedAction pa = new GetPropertyAction("os.name");
String osname = (String) AccessController.doPrivileged(pa);
if ("SunOS".equals(osname)) {//1銆佸鏋淪unOS
return new sun.nio.ch.DevPollSelectorProvider();
}
//2銆丩inux 鍐呮牳>=2.6
// use EPollSelectorProvider for Linux kernels >= 2.6
if ("Linux".equals(osname)) {
pa = new GetPropertyAction("os.version");
String osversion = (String) AccessController
.doPrivileged(pa);
String[] vers = osversion.split("\\.", 0);
if (vers.length >= 2) {
try {
int major = Integer.parseInt(vers[0]);
int minor = Integer.parseInt(vers[1]);
if (major > 2 || (major == 2 && minor >= 6)) {
return new sun.nio.ch.EPollSelectorProvider();
}
} catch (NumberFormatException x) {
// format not recognized
}
}
}
return new sun.nio.ch.PollSelectorProvider();
}



鍏?span mce_style="white-space: pre;" style="white-space: pre; ">create鏂規硶鏍規嵁涓嶅悓鐨勬搷浣滅郴緇熸瀯寤轟笉鍚岀殑SelectorProvider錛屼富瑕佸垎涓簎nix銆乴inux錛宱ther錛宭inux閽堝鍐呮牳2.6浠ヤ笂

閫氳繃epoll銆?/span>鑾峰彇緋葷粺鐜涓殑os.name銆乷s.version瑙傚療涓嬩笉鍚屽鉤鍙扮殑緇嗚妭銆?/span>


System.out.println(System.getProperty("os.name"));
System.out.println(System.getProperty("os.version"));
System.out.println(java.nio.channels.spi.SelectorProvider.provider());


 閫氳繃鍦ㄤ笉鍚岀殑鎿嶄綔緋葷粺涓婃墽琛屽涓嬩唬鐮佸嵆鍙尯鍒嗭細

win XP sp3錛?/p>


 鍐欓亾
Windows XP
5.1
sun.nio.ch.WindowsSelectorProvider@1fb8ee3


 ubuntu 11.04錛?/p>


 鍐欓亾
Linux
2.6.38-8-generic
sun.nio.ch.EPollSelectorProvider@160c21a


JDK瀵筶inux鍐呮牳2.6浠ヤ笂鐗堟湰榛樿閲囩敤epoll錛孡inux涓嬫ц兘寰楀埌涓瀹氬箙搴︽彁鍗囥?/p>



鍙傝冿細

DefaultSelectorProvider婧愮爜錛?/span>http://www.java2s.com/Open-Source/Java-Document/6.0-JDK-Platform/solaris/sun/nio/ch/DefaultSelectorProvider.java.htm



絀虹櫧 2011-05-10 20:53 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 亚洲精品乱码久久久久久下载 | 亚洲综合一区二区精品导航| 精品国产香蕉伊思人在线在线亚洲一区二区 | 老司机午夜性生免费福利 | 亚洲AV无码AV日韩AV网站| 中文无码亚洲精品字幕| 久久综合久久综合亚洲| 亚洲综合小说另类图片动图| 亚洲熟妇无码AV| WWW国产亚洲精品久久麻豆| 亚洲AV无码一区二区三区性色 | 国产亚洲福利精品一区二区| 免费无码午夜福利片| 国产精品偷伦视频免费观看了| 香蕉免费一级视频在线观看| 成人性做爰aaa片免费看| 久久久久高潮毛片免费全部播放| 亚洲毛片免费视频| 成人免费视频试看120秒| 又黄又爽的视频免费看| 亚洲五月午夜免费在线视频| 亚洲国产精品SSS在线观看AV| 91久久亚洲国产成人精品性色| 亚洲一卡二卡三卡| 青青青亚洲精品国产| 成人免费乱码大片A毛片| 91大神免费观看| 操美女视频免费网站| 亚洲精品乱码久久久久久不卡| 亚洲国产第一站精品蜜芽| 久久精品国产亚洲αv忘忧草 | 色在线亚洲视频www| 老司机精品视频免费| 精品视频一区二区三区免费| 2021久久精品免费观看| 国产精品国产免费无码专区不卡 | 久久亚洲国产精品| 亚洲人成网站色7799| 久久er国产精品免费观看8| 1000部羞羞禁止免费观看视频 | 国内自产拍自a免费毛片|