package com.abin.lir.axis2.client;
//方法二
//方法三
少年阿賓那些青春的歲月 |
//方法一: package com.abin.lir.axis2.client; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; public class UserClient { public static void main(String[] args) { try { ServiceClient sc = new ServiceClient(); Options opts = new Options(); opts.setTo(new EndpointReference("http://localhost:9090/universal/services/play")); opts.setAction("urn:echo"); opts.setTimeOutInMilliSeconds(10000); sc.setOptions(opts); OMElement res = sc.sendReceive(createPayLoad()); System.out.println(res); } catch (AxisFault e) { e.printStackTrace(); } } public static OMElement createPayLoad(){ OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace("http://localhost:9090/universal/services/play", "nsl"); OMElement method = fac.createOMElement("getPassengerInfos",omNs); OMElement value = fac.createOMElement("userID",omNs); value.setText("1024"); method.addChild(value); return method; } } //方法二 package com.abin.lir.axis2.client; import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; public class RPCClient { public static void main(String[] args) throws AxisFault { // 使用RPC方式調(diào)用WebService RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); // 指定調(diào)用WebService的URL EndpointReference targetEPR = new EndpointReference( "http://localhost:9090/universal/services/play"); options.setTo(targetEPR); // 指定方法的參數(shù)值 Object[] requestParam = new Object[] {"1024"}; // 指定方法返回值的數(shù)據(jù)類型的Class對象 Class[] responseParam = new Class[] {String.class}; // 指定要調(diào)用的getGreeting方法及WSDL文件的命名空間 QName requestMethod = new QName("http://localhost:9090/universal/services/play", "getPassengerInfos"); // 調(diào)用方法并輸出該方法的返回值 try { System.out.println(serviceClient.invokeBlocking(requestMethod, requestParam, responseParam)[0]); } catch (AxisFault e) { e.printStackTrace(); } } } //方法三 package com.abin.lir.axis2.client; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.axis2.Constants; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; public class AXIOMClient { private static EndpointReference targetEPR = new EndpointReference( "http://localhost:9090/universal/services/play"); public static OMElement getPassengerInfos(String symbol) { OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace( "http://localhost:9090/universal/services/play", "tns"); OMElement method = fac.createOMElement("getPassengerInfos", omNs); OMElement value = fac.createOMElement("userID", omNs); value.addChild(fac.createOMText(value, symbol)); method.addChild(value); return method; } public static void main(String[] args) { try { OMElement getPassenger = getPassengerInfos("1024"); Options options = new Options(); options.setTo(targetEPR); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); ServiceClient sender = new ServiceClient(); sender.setOptions(options); OMElement result = sender.sendReceive(getPassenger); String response = result.getFirstElement().getText(); System.err.println("Current passengers: " + response); } catch (Exception e) { e.printStackTrace(); } } } http://blog.csdn.net/leo821031/article/details/1545974
Axis2生成客戶端方式 基于StockQuoteService類創(chuàng)建客戶端的四種方式 構(gòu)建基于AXIOM的客戶端; 使用Axis2 Databinding Frame work(ADB)生成客戶端; 使用XMLBeans生成客戶端; 使用JiBX生成客戶端。 ADB:最簡單的生成Axis客戶端的方法。大部分情況下,這些主要的類都會以內(nèi)部類的形式創(chuàng)建在stub類中。It is not meant to be a full schema bindingapplication, and has difficulty with structures such as XML Schema element extensions and restrictions。 對于簡單應用來說ADB已經(jīng)夠用了,如果想用更加強大更加靈活的功能,那么你可能需要使用其他兩種方式。 Axis2提供的四種調(diào)用模式 Web services可以用來為用戶提供廣泛的功能,從簡單的、少時間消耗的功能到多時間消耗的業(yè)務服務。當我們使用(調(diào)用客戶端的應用程序)這些Web Service時,我們不能用簡單的調(diào)用機制來針對那些對時間消耗有很大要求的服務操作。例如,如果我們使用一個簡單的傳輸通道(如HTTP)并使用IN-OUT模式來調(diào)用一個需要很長時間來完成的Web Service,那么多數(shù)情況下,我們得到的結(jié)果將是"connection time outs"。另一方面,如果我們從一個簡單的客戶端應用程序調(diào)用一個同步的服務,使用"blocking"的客戶端API將會降低客戶端應用程序的性能。現(xiàn)在來分析一下一些常用的服務調(diào)用形式。
許多Web Service引擎提供給客戶Blocking和Non-Blocking的客戶端APIs。 1)Blocking API-一旦服務被啟用,客戶端的應用程序?qū)⒈粧炱穑钡給peration被執(zhí)行完畢(表現(xiàn)為收到一個response或fault),才能重新獲得控制權(quán)。這是調(diào)用Web Service最簡單的方式,并且這種方式適用于多數(shù)業(yè)務情形。 2)Non-Blocking API-這是一個回叫或輪詢機制的API。因此,一旦服務被起用,客戶端應用程序馬上得到控制權(quán),通過使用一個callback對象來獲得response。這種方式使得客戶端應用程序可以很方便的同步啟用多個Web Service。 這兩種機制都是工作在API層面上的。稱將通過使用Non-Blocking API而產(chǎn)生的異步行為方式為API Level 異步。這兩種機制都使用單一的傳輸連接來發(fā)送request和接收response。它們的性能遠遠落后于使用兩個傳輸連接來發(fā)送request和接收response(不管是單工還是雙工)。所以這兩種機制都不能解決需要長時間處理的事務的傳輸問題(在operation處理完成之前,很有可能你的連接已經(jīng)超時了)。一種可能的解決方法是使用兩個獨立的傳輸連接來發(fā)送和接收request&response。這種異步行為,我們稱為Transport Level 異步。 通過組合API Level異步和Transport Level 異步,我們可以得到四種調(diào)用模式。如下所示。
Axis2提供了所有上述4種調(diào)用Web Service的實現(xiàn)方式。 node.selectNodes("http://xml"); 問題補充:不是很明白 nodename 選取此節(jié)點的所有子節(jié)點
Java所有的類都具有線程的潛力,Java賦予的每個對象一個鎖,在計算機內(nèi)部工作在同一時間,只有一個對象可以持有鎖,也就是說程序在同一時間只有一個程序可以運行,這里我把對象比作是一個小的程序。而多處理器,那么就另當別論了。
在這里我們首先學習一下公共方法wait,notify,notifyAll。 wait方法可以使在當前線程的對象等待,直到別的線程調(diào)用此對象的notify或notifyAll方法(注意:調(diào)用的是此對象的notify和notifyAll),并且當前運行的線程必須具有此對象的對象監(jiān)視器 package com.abin.lee.thread.thread; public class CarryTask extends Thread {
package com.abin.lee.thread.thread; public class CarryWait { } http://www.iteye.com/topic/1124814 今天試著把SpringMVC與fastjson整合了下,經(jīng)測試也能解決json含中文亂碼的問題,特此分享之。我也是初用,詳細文檔請見官網(wǎng)。
public class MappingFastJsonHttpMessageConverter extends AbstractHttpMessageConverter<Object> { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); private SerializerFeature[] serializerFeature; public SerializerFeature[] getSerializerFeature() { return serializerFeature; } public void setSerializerFeature(SerializerFeature[] serializerFeature) { this.serializerFeature = serializerFeature; } public MappingFastJsonHttpMessageConverter() { super(new MediaType("application", "json", DEFAULT_CHARSET)); } @Override public boolean canRead(Class<?> clazz, MediaType mediaType) { return true; } @Override public boolean canWrite(Class<?> clazz, MediaType mediaType) { return true; } @Override protected boolean supports(Class<?> clazz) { throw new UnsupportedOperationException(); } @Override protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int i; while ((i = inputMessage.getBody().read()) != -1) { baos.write(i); } return JSON.parseArray(baos.toString(), clazz); } @Override protected void writeInternal(Object o, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { String jsonString = JSON.toJSONString(o, serializerFeature); OutputStream out = outputMessage.getBody(); out.write(jsonString.getBytes(DEFAULT_CHARSET)); out.flush(); } } SpringMVC關鍵配置: <mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <!-- fastjosn spring support --> <bean id="jsonConverter" class="com.alibaba.fastjson.spring.support.MappingFastJsonHttpMessageConverter"> <property name="supportedMediaTypes" value="application/json" /> <property name="serializerFeature"> <list> <value>WriteMapNullValue</value> <value>QuoteFieldNames</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> http://xyly624.blog.51cto.com/842520/896704 //實例一,這里面用到了信號量Semaphore和FutureTask
package net.abin.lee.mythread.callable; import java.util.concurrent.Callable; public class FutureGo implements Callable<String> { public String call() throws InterruptedException { package net.abin.lee.mythread.callable; import java.util.concurrent.Callable; public class FutureTaskTest { }
package com.abin.lee.junit.httpasyncclient.example; import java.util.List; package com.abin.lee.junit.httpasyncclient.service; import java.io.BufferedWriter; import javax.servlet.ServletOutputStream; public class HttpAsyncClientService extends HttpServlet{ @SuppressWarnings("rawtypes") <servlet> <servlet-name>HttpAsyncClientService</servlet-name> <servlet-class>com.abin.lee.junit.httpasyncclient.service.HttpAsyncClientService</servlet-class> </servlet> <servlet-mapping> <servlet-name>HttpAsyncClientService</servlet-name> <url-pattern>/HttpAsyncClientService</url-pattern> </servlet-mapping> package com.abin.lee.junit.httpasyncclient.example; import java.util.concurrent.CountDownLatch; import org.apache.http.HttpResponse; public class CreateAsyncClientHttpExchangeFutureCallback { httpclient.start(); public void completed(final HttpResponse response) { public void failed(final Exception ex) { public void cancelled() { }); |