2013年7月23日 星期二

[ Java 代碼範本 ] 用java設定、取用剪貼簿內容

來源自 這裡 
Preface: 
平常在使用 Ctrl+C (複製到剪貼簿), Ctrl+P (取出剪貼簿中的內容) 也可以使用 Java 代碼進行操作. 

Sample Code: 
- CutBook.java 
  1. package test;  
  2.   
  3. import java.awt.Toolkit;  
  4. import java.awt.datatransfer.Clipboard;  
  5. import java.awt.datatransfer.ClipboardOwner;  
  6. import java.awt.datatransfer.DataFlavor;  
  7. import java.awt.datatransfer.StringSelection;  
  8. import java.awt.datatransfer.Transferable;  
  9.   
  10. public class CutBook implements ClipboardOwner{  
  11.     private Clipboard clipboard;  
  12.   
  13.     public CutBook() {  
  14.         CutBookInit();  
  15.     }  
  16.   
  17.     public void CutBookInit(){  
  18.         clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();  
  19.     }  
  20.   
  21.     /** 
  22.      * 設定剪貼內容 
  23.      * @param str 
  24.      */      
  25.     public void setBookContents(String str){  
  26.         StringSelection contents = new StringSelection(str);  
  27.         clipboard.setContents(contents, this);  
  28.     }  
  29.   
  30.     /** 
  31.      * 取出剪貼內容 
  32.      * @return 
  33.      */  
  34.     public String getBookContents(){  
  35.         Transferable content = clipboard.getContents(this);  
  36.         try{  
  37.             return (String) content.getTransferData(DataFlavor.stringFlavor);  
  38.         }catch(Exception e){  
  39.             e.printStackTrace();  
  40.             //System.out.println(e);  
  41.         }  
  42.         return null;  
  43.     }  
  44.   
  45.     public void lostOwnership(Clipboard clipboard, Transferable contents) {  
  46.         //System.out.println("lostOwnership...");  
  47.     }  
  48.       
  49.     /** 
  50.      * @param args 
  51.      */  
  52.     public static void main(String[] args) {  
  53.         //印出目前剪貼簿內容  
  54.           
  55.         CutBook cb = new CutBook();  
  56.         System.out.println(cb.getBookContents());  
  57.   
  58.     }  
  59.   
  60. }  
接著你使用 Ctrl+C 去複製一段內容, 在執行上面的程式便可以將剛剛複製的內容取出並列印在 Console 中. 

2013年7月14日 星期日

[ Java 代碼範本 ] How do I use a host name to look up an IP address

來源自 這裡
Preface:
The InetAddress class can be used to perform Domain Name Server (DNS) lookups. For example, you can call the static InetAddress.getByName("www.teamcakes.com")to retrieve an InetAddress object for 'www.teamcakes.com'. This object would contain the canonical name, host name, and ip address of 'www.teamcakes.com'.

The DnsTest class below demonstrates InetAddress.getLocalHost(), which obtains an Internet Address for your local host. It also demonstratesInetAddress.getByName("www.google.com"), which gives an Internet Address object for 'www.google.com'. However, it should be noted that a DNS name can map to multiple servers, and the InetAddress.getAllByName("www.google.com") call retrieves an array of InetAddress objects that represent all of the 'www.google.com' servers retrieved from DNS.

DnsTest 範例代碼:
  1. package test;  
  2.   
  3. import java.net.InetAddress;  
  4. import java.net.UnknownHostException;  
  5.   
  6. public class DnsTest {  
  7.     public static void main(String[] args) {  
  8.         try {  
  9.             InetAddress inetAddress = InetAddress.getLocalHost();  
  10.             displayStuff("local host", inetAddress);  
  11.             System.out.print("--------------------------");  
  12.             inetAddress = InetAddress.getByName("www.google.com");  
  13.             displayStuff("www.google.com", inetAddress);  
  14.             System.out.print("--------------------------");  
  15.             InetAddress[] inetAddressArray = InetAddress.getAllByName("www.google.com");  
  16.             for (int i = 0; i < inetAddressArray.length; i++) {  
  17.                 displayStuff("www.google.com #" + (i + 1), inetAddressArray[i]);  
  18.             }  
  19.         } catch (UnknownHostException e) {  
  20.             e.printStackTrace();  
  21.         }  
  22.     }  
  23.   
  24.     public static void displayStuff(String whichHost, InetAddress inetAddress) {  
  25.         System.out.println("--------------------------");  
  26.         System.out.println("Which Host:" + whichHost);  
  27.         System.out.println("Canonical Host Name:" + inetAddress.getCanonicalHostName());  
  28.         System.out.println("Host Name:" + inetAddress.getHostName());  
  29.         System.out.println("Host Address:" + inetAddress.getHostAddress());  
  30.     }  
  31. }  
輸出結果:
--------------------------
Which Host:local host
Canonical Host Name:John-Lee
Host Name:John-Lee
Host Address:192.168.6.1
----------------------------------------------------
Which Host:www.google.com
Canonical Host Name:tf-in-f99.1e100.net
Host Name:www.google.com
Host Address:173.194.72.99
----------------------------------------------------
Which Host:www.google.com #1
Canonical Host Name:tf-in-f99.1e100.net
Host Name:www.google.com
Host Address:173.194.72.99
--------------------------
...
--------------------------
Which Host:www.google.com #6
Canonical Host Name:tf-in-f105.1e100.net
Host Name:www.google.com
Host Address:173.194.72.105
--------------------------
Which Host:www.google.com #7
Canonical Host Name:2404:6800:4008:c01:0:0:0:6a
Host Name:www.google.com
Host Address:2404:6800:4008:c01:0:0:0:6a

This message was edited 2 times. Last update was at 15/07/2013 10:24:50

[ Java 文章收集 ] HttpClient4.2.X 升級入門+ http連接池使用

來源自 這裡
Preface:
在一次服務器異常的排查過程當中,我們決定使用HttpClient4.X替代HttpClient3.X或者HttpConnection。為什麼使用HttpClient4?主要是HttpConnection沒有連接池的概念,多少次請求就會建立多少個IO,在訪問量巨大的情況下服務器的IO可能會耗盡。HttpClient3 也有連接池的東西在裡頭,使用 MultiThreadedHttpConnectionManager,大致過程如下:
  1. MultiThreadedHttpConnectionManager connectionManager =  new  MultiThreadedHttpConnectionManager();    
  2. HttpClient client =  new  HttpClient(connectionManager);... //在某個線程中。    
  3. GetMethod get =  new  GetMethod( "http://jakarta.apache.org/" );    
  4. try  {    
  5.     client.executeMethod(get); // print response to stdout    
  6.     System.out.println(get.getResponseBodyAsStream());    
  7. }  finally  {    
  8.     // be sure the connection is released back to the connection     
  9.     managerget.releaseConnection();    
  10. }   
可以看出來,它的方式與jdbc連接池的使用方式相近,這裡比較麻煩的就是需要手動調用 releaseConnection 去釋放連接。對每一個 HttpClient.executeMethod 須有一個 method.releaseConnection() 與之匹配!

HttpClient4.x 連接池:
這裡使用4.2.x的版本實現了一個簡單的 HttpConnectionManager,代碼如下:
  1. package nlg.utils;  
  2.   
  3. import org.apache.http.client.HttpClient;  
  4. import org.apache.http.impl.client.DefaultHttpClient;  
  5. import org.apache.http.impl.conn.PoolingClientConnectionManager;  
  6. import org.apache.http.params.BasicHttpParams;  
  7. import org.apache.http.params.HttpConnectionParams;  
  8. import org.apache.http.params.HttpParams;  
  9.   
  10. public class HttpConnectionManager {  
  11.     private static HttpParams httpParams;  
  12.     private static PoolingClientConnectionManager connectionManager;  
  13.   
  14.     /** 
  15.      * 最大连接数 
  16.      */  
  17.     public final static int MAX_TOTAL_CONNECTIONS = 2000;  
  18.   
  19.     /** 
  20.      * 每个路由最大连接数 
  21.      */  
  22.     public final static int MAX_ROUTE_CONNECTIONS = 400;  
  23.     /** 
  24.      * 连接超时时间 
  25.      */  
  26.     public final static int CONNECT_TIMEOUT = 10000;  
  27.     /** 
  28.      * 读取超时时间 
  29.      */  
  30.     public final static int READ_TIMEOUT = 10000;  
  31.   
  32.     static {          
  33.         httpParams = new BasicHttpParams();   
  34.         HttpConnectionParams.setConnectionTimeout(httpParams, CONNECT_TIMEOUT);  
  35.         HttpConnectionParams.setSoTimeout(httpParams, READ_TIMEOUT);          
  36.           
  37.         connectionManager = new PoolingClientConnectionManager();  
  38.         connectionManager.setMaxTotal(MAX_TOTAL_CONNECTIONS);  
  39.         connectionManager.setDefaultMaxPerRoute(MAX_ROUTE_CONNECTIONS);  
  40.     }  
  41.   
  42.     public static HttpClient GetHttpClient() {  
  43.         return new DefaultHttpClient(connectionManager, httpParams);  
  44.     }  
  45.   
  46.     public static synchronized void Shutdown()  
  47.     {  
  48.         if(DHC!=null)  
  49.         {  
  50.             DHC.getConnectionManager().shutdown();  
  51.             DHC = null;  
  52.         }  
  53.     }  
  54. }  
接著便可以如下使用上面建立的類別. 另外 HttpClient4 使用我們常用的 InputStream.close() 來確認連接關閉(4.1版本之前使用entity.consumeContent()來確認內容已經被消耗關閉連接):
  1. package nlg.test;  
  2.   
  3. import nlg.utils.HttpConnectionManager;  
  4.   
  5. import org.apache.http.HttpEntity;  
  6. import org.apache.http.HttpResponse;  
  7. import org.apache.http.client.HttpClient;  
  8. import org.apache.http.client.methods.HttpGet;  
  9. import org.apache.http.protocol.BasicHttpContext;  
  10. import org.apache.http.protocol.HttpContext;  
  11. import org.apache.http.util.EntityUtils;  
  12.   
  13. public class ClientMultiThreadedEx {  
  14.     public static void main(String args[]) throws Exception  
  15.     {  
  16.         try {  
  17.             // create an array of URIs to perform GETs on  
  18.             String[] urisToGet = {  
  19.                 "http://hc.apache.org/",  
  20.                 "http://hc.apache.org/httpcomponents-core-ga/",  
  21.                 "http://hc.apache.org/httpcomponents-client-ga/",  
  22.                 "http://svn.apache.org/viewvc/httpcomponents/"  
  23.             };  
  24.   
  25.             // create a thread for each URI  
  26.             GetThread[] threads = new GetThread[urisToGet.length];  
  27.             for (int i = 0; i < threads.length; i++) {  
  28.                 HttpGet httpget = new HttpGet(urisToGet[i]);  
  29.                 threads[i] = new GetThread(HttpConnectionManager.getHttpClient(), httpget, i + 1);  
  30.             }  
  31.   
  32.             // start the threads  
  33.             for (int j = 0; j < threads.length; j++) {  
  34.                 threads[j].start();  
  35.             }  
  36.   
  37.             // join the threads  
  38.             for (int j = 0; j < threads.length; j++) {  
  39.                 threads[j].join();  
  40.             }  
  41.   
  42.         } finally {  
  43.             // When HttpClient instance is no longer needed,  
  44.             // shut down the connection manager to ensure  
  45.             // immediate deallocation of all system resources  
  46.             HttpConnectionManager.Shutdown();  
  47.         }  
  48.     }  
  49.       
  50.       
  51.     /** 
  52.      * A thread that performs a GET. 
  53.      */  
  54.     static class GetThread extends Thread {  
  55.   
  56.         private final HttpClient httpClient;  
  57.         private final HttpContext context;  
  58.         private final HttpGet httpget;  
  59.         private final int id;  
  60.   
  61.         public GetThread(HttpClient httpClient, HttpGet httpget, int id) {  
  62.             this.httpClient = httpClient;  
  63.             this.context = new BasicHttpContext();  
  64.             this.httpget = httpget;  
  65.             this.id = id;  
  66.         }  
  67.   
  68.         /** 
  69.          * Executes the GetMethod and prints some status information. 
  70.          */  
  71.         @Override  
  72.         public void run() {  
  73.   
  74.             System.out.println(id + " - about to get something from " + httpget.getURI());  
  75.   
  76.             try {  
  77.   
  78.                 // execute the method  
  79.                 HttpResponse response = httpClient.execute(httpget, context);  
  80.   
  81.                 System.out.println(id + " - get executed");  
  82.                 // get the response body as an array of bytes  
  83.                 HttpEntity entity = response.getEntity();  
  84.                 if (entity != null) {  
  85.                     byte[] bytes = EntityUtils.toByteArray(entity);  
  86.                     System.out.println(id + " - " + bytes.length + " bytes read");  
  87.                 }  
  88.   
  89.             } catch (Exception e) {  
  90.                 httpget.abort();  
  91.                 System.out.println(id + " - error: " + e);  
  92.             }  
  93.         }  
  94.   
  95.     }  
  96. }  
Supplement:
HttpClient 3.1 Threading issue
Class PoolingClientConnectionManager
Manages a pool of client connections and is able to service connection requests from multiple execution threads. Connections are pooled on a per route basis. A request for a route which already the manager has persistent connections for available in the pool will be services by leasing a connection from the pool rather than creating a brand new connection.

This message was edited 9 times. Last update was at 14/07/2013 16:08:34

[ Python 常見問題 ] How to shift a datetime object by 12 hours in python

Source From   Here   Question   Datetime   objects hurt my head for some reason. I am writing to figure out   how to shift a date time obje...