久久久人人人,精品免费一区二区,久久国产三级精品http://www.aygfsteel.com/waynemao/zh-cnThu, 19 Jun 2025 09:49:38 GMTThu, 19 Jun 2025 09:49:38 GMT60JMS (2)http://www.aygfsteel.com/waynemao/archive/2008/09/14/228902.htmlwaynemaowaynemaoSun, 14 Sep 2008 08:40:00 GMThttp://www.aygfsteel.com/waynemao/archive/2008/09/14/228902.htmlhttp://www.aygfsteel.com/waynemao/comments/228902.htmlhttp://www.aygfsteel.com/waynemao/archive/2008/09/14/228902.html#Feedback0http://www.aygfsteel.com/waynemao/comments/commentRss/228902.htmlhttp://www.aygfsteel.com/waynemao/services/trackbacks/228902.html異步隊列消息的接收有一點區別,但發送的代碼不用改變:

 1@Stateless
 2public class JMSReceiveBean implements JMSReceiveRemote {
 3
 4    @Resource(name = "jms/Queue")
 5    private Queue queue;
 6    @Resource(name = "jms/ConnectionFactory")
 7    private ConnectionFactory queueFactory;
 8
 9    private void receiveJMSMessageFromQueue() throws Exception {
10        Connection connection = null;
11        Session session = null;
12        connection = queueFactory.createConnection();
13        session = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
14        MessageConsumer consumer = session.createConsumer(queue);
15        consumer.setMessageListener(new MessageListener() {
16
17            public void onMessage(Message message) {
18                TextMessage msg = (TextMessage) message;
19                try {
20                    String txt = msg.getText();
21                    Logger.getLogger(JMSReceiveBean.class.getName()).log(Level.SEVERE, txt);
22                }
 catch (Exception ex) {
23                    ex.printStackTrace();
24                }

25            }

26        }
);
27        
28        connection.start();
29    }

30
31    public void receiveMessage() {
32        try {
33            receiveJMSMessageFromQueue();
34        }
 catch (Exception ex) {
35            Logger.getLogger(JMSReceiveBean.class.getName()).log(Level.SEVERE, null, ex);
36        }

37    }

38}

在15行消息使用者上設置了一個消息監聽器,而沒有使用同步的receive方法。由于這是異步接收消息,程序并沒有處于阻塞狀態,為了避免在接收到消息之前連接終止,所以在28行之后并沒有關閉Connection,實際上這一步是不可缺少的。

waynemao 2008-09-14 16:40 發表評論
]]>
JMS (1)http://www.aygfsteel.com/waynemao/archive/2008/09/14/228843.htmlwaynemaowaynemaoSat, 13 Sep 2008 21:31:00 GMThttp://www.aygfsteel.com/waynemao/archive/2008/09/14/228843.htmlhttp://www.aygfsteel.com/waynemao/comments/228843.htmlhttp://www.aygfsteel.com/waynemao/archive/2008/09/14/228843.html#Feedback0http://www.aygfsteel.com/waynemao/comments/commentRss/228843.htmlhttp://www.aygfsteel.com/waynemao/services/trackbacks/228843.html閱讀全文

waynemao 2008-09-14 05:31 發表評論
]]>
EJB3 (7)http://www.aygfsteel.com/waynemao/archive/2008/09/13/228705.htmlwaynemaowaynemaoSat, 13 Sep 2008 03:14:00 GMThttp://www.aygfsteel.com/waynemao/archive/2008/09/13/228705.htmlhttp://www.aygfsteel.com/waynemao/comments/228705.htmlhttp://www.aygfsteel.com/waynemao/archive/2008/09/13/228705.html#Feedback0http://www.aygfsteel.com/waynemao/comments/commentRss/228705.htmlhttp://www.aygfsteel.com/waynemao/services/trackbacks/228705.html有狀態會話bean的開發,首先創建遠程接口:

@Remote
public interface HelloRemote {

    String sayHi();

    
void init(String name);
    
    
void remove();
    
}

接著開發有狀態會話bean:
@Stateful
public class HelloBean implements HelloRemote {
    
private String name;
    
private Logger log = Logger.getLogger(this.getClass().getName());
    
    
public String sayHi() {
        
return "Hi " + name;
    }


    
public void init(String name) {
        
this.name = name;
    }

    
    @PostConstruct
    
public void postConstrut(){
        log.info(
"create " + this);
    }

    
    @PreDestroy
    
public void preDestory(){
        log.info(
"destory " + this);
    }

    
    @PostActivate
    
public void postActivate(){
        log.info(
"activate " + this);
    }

    
    @PrePassivate
    
public void prePassivate(){
        log.info(
"passivate " + this);
    }

    
    @Remove
    
public void remove(){
        log.info(
"remove " + this);
    }

}

@Stateful注釋表明這是一個有狀態會話bean,其他的注釋是管理會話bean的生命周期。@PostConstruct注釋表明方法將會在bean實例化并完成依賴注入后由容器調用此方法;@PreDestory注釋表示方法會在容器刪除bean實例前由容器調用;以上兩個注釋所有的EJB(包括MDB)都可以用。@PostActivate注釋表示容器在激活bean后調用此方法;@PrePassivate注釋表示容器在鈍化bean前調用此方法;以上兩個注釋是有狀態會話bean所特有。@Remove注釋也是有狀態會話bean所特有,也是用戶唯一可以能控制的生命周期方法,一旦用戶在客戶端調用此方法,容器將刪除bean實例。接著看客戶端的測試代碼:
<%
           InitialContext ctx 
= new InitialContext();
           HelloRemote helloBean 
= (HelloRemote)ctx.lookup(HelloRemote.class.getName());
           helloBean.init(
"Tom");
           out.println(helloBean.sayHi());
           helloBean.remove();
%>

很簡單,只是多調用了一個remove方法。

waynemao 2008-09-13 11:14 發表評論
]]>
EJB3 (6)http://www.aygfsteel.com/waynemao/archive/2008/09/13/228692.htmlwaynemaowaynemaoFri, 12 Sep 2008 20:36:00 GMThttp://www.aygfsteel.com/waynemao/archive/2008/09/13/228692.htmlhttp://www.aygfsteel.com/waynemao/comments/228692.htmlhttp://www.aygfsteel.com/waynemao/archive/2008/09/13/228692.html#Feedback0http://www.aygfsteel.com/waynemao/comments/commentRss/228692.htmlhttp://www.aygfsteel.com/waynemao/services/trackbacks/228692.htmlWeb服務客戶端可以通過bean的Web服務端點實現類來訪問無狀態會話Bean。在默認情況下,bean類中的所有公共方法對于Web服務客戶端都是可訪問的。@WebMethod注釋可以自定義Web服務方法,一旦在公共方法上使用該注釋,那么其他沒使用@WebMethod注釋的方法將不會對Web服務客戶端展現。
首先新建一個EJB模塊,然后在EJB模塊上新建一個WebService,代碼如下:

@WebService()
@Stateless()
public class Dog {
    
    @WebMethod(operationName 
= "ganr")
    
public String ganr() {
        
return "Wo-Wo-Wo";
    }


}

Dog類同時使用了@WebService和@Stateless注釋,web服務端點只能是無狀態會話bean,web服務本身就是無狀態的。我們還用@WebMethod注釋向Web服務客戶端公開了一個ganr方法,完成后打包部署。
接著我們創建一個Web模塊,然后在Web項目上右鍵,選新建--Web服務客戶端,指定項目或者WSDL url。接著新建一個Servlet,然后右鍵調用Web服務操作,我們找到Dog服務的garn方法點確定,代碼自動生成:
@WebServiceRef(wsdlLocation = "http://localhost:8080/DogService/Dog?wsdl")
    
private DogService service;

@WebServiceRef注釋聲明了一個到Web服務的引用
try // Call Web Service Operation

                ejb.Dog port 
= service.getDogPort();
                
// TODO process result here
                java.lang.String result = port.ganr();
                out.println(
"Result = " + result);
            }
 catch (Exception ex) {
                
// TODO handle custom exceptions here
            }

service.getDogPort()方法獲取到服務的一個代理,也稱為端口。接著看jsp:
  <%
    
try {
    ejb.DogService service 
= new ejb.DogService();
    ejb.Dog port 
= service.getDogPort();
    
// TODO process result here
    java.lang.String result = port.ganr();
    out.println(
"Result = "+result);
    }
 catch (Exception ex) {
    
// TODO handle custom exceptions here
    }

    
%>

發現只有一點點不同,完成后部署web應用并運行測試。

我們還可以通過一個現有的會話Bean創建Web服務,還是拿HelloWorld舉例,首先創建一個遠程接口:
@Remote
public interface HelloRemote {

    String sayHi(String name);
}

再創建一個無狀態會話bean,先已經說過了,只能是無狀態會話bean:
@Stateless
public class HelloBean implements HelloRemote {

    
public String sayHi(String name) {
        
return "Hi " + name; 
    }

}

然后新建一個Web服務Hello,只不過在彈出窗口中要選中“通過現有會話bean創建Web服務”單選框,并瀏覽指定HelloBean,代碼自動完成:
@WebService()
@Stateless()
public class Hello {
    @EJB
    
private HelloRemote ejbRef;
    
// Add business logic below. (Right-click in editor and choose
    
// "Web Service > Add Operation"

    @WebMethod(operationName 
= "sayHi")
    
public String sayHi(@WebParam(name = "name")String name) {
        
return ejbRef.sayHi(name);
    }

}

我們看到Hello服務里引用了一個HelloRemote接口,并發現遠程接口公開的方法也被Hello服務公開,完成后打包部署EJB模塊。接著在Web服務客戶端測試,這和之前的步驟一樣,不再贅述,直接看代碼吧:
@WebServiceRef(wsdlLocation = "http://localhost:8080/HelloService/Hello?wsdl")
    
private HelloService service;

    
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {
        response.setContentType(
"text/html;charset=UTF-8");
        PrintWriter out 
= response.getWriter();
        
try {
            
try // Call Web Service Operation

                ejb.Hello port 
= service.getHelloPort();
                
// TODO initialize WS operation arguments here
                java.lang.String name = "Tom";
                
// TODO process result here
                java.lang.String result = port.sayHi(name);
                out.println(
"Result = " + result);
            }
 catch (Exception ex) {
                
// TODO handle custom exceptions here
            }

 

<%
    
try {
    ejb.HelloService service 
= new ejb.HelloService();
    ejb.Hello port 
= service.getHelloPort();
     
// TODO initialize WS operation arguments here
    java.lang.String name = "Tom";
    
// TODO process result here
    java.lang.String result = port.sayHi(name);
    out.println(
"Result = "+result);
    }
 catch (Exception ex) {
    
// TODO handle custom exceptions here
    }

    
%>

發現一個問題,一個應用上不能新建兩個Web服務客戶端(Dog和Hello),只有一個能有效使用(只找到一份工件),這是為什么?

waynemao 2008-09-13 04:36 發表評論
]]>
WebService (1)http://www.aygfsteel.com/waynemao/archive/2008/09/13/228691.htmlwaynemaowaynemaoFri, 12 Sep 2008 18:31:00 GMThttp://www.aygfsteel.com/waynemao/archive/2008/09/13/228691.htmlhttp://www.aygfsteel.com/waynemao/comments/228691.htmlhttp://www.aygfsteel.com/waynemao/archive/2008/09/13/228691.html#Feedback0http://www.aygfsteel.com/waynemao/comments/commentRss/228691.htmlhttp://www.aygfsteel.com/waynemao/services/trackbacks/228691.html新建web項目,然后創建一個WEB服務:

@WebService()
public class Hello {
    @WebMethod(operationName 
= "sayHi")
    
public String sayHi(@WebParam(name = "name")String name) {
       
return "Hi " + name;
    }

}


可以在源圖上右鍵,選Web服務--添加操作,也可以在設計圖上直接添加操作。@WebService標注表明該類是一個web服務,展現給web服務客戶端的業務方法必須使用@WebMethod標注來表示。打包部署該web應用,web服務自動會發布。可以在glassfish應用服務器上找到該web服務,直接測試或者查看服務器生成的WSDL

<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3.1-hudson-417-SNAPSHOT. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3.1-hudson-417-SNAPSHOT. -->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservice/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservice/" name="HelloService">
    
<types>
        
<xsd:schema>
            
<xsd:import namespace="http://webservice/" schemaLocation="http://localhost:8080/WebServiceApp/HelloService?xsd=1">
            
</xsd:import>
        
</xsd:schema>
    
</types>
    
<message name="sayHi">
        
<part name="parameters" element="tns:sayHi">
        
</part>
    
</message>
    
<message name="sayHiResponse">
        
<part name="parameters" element="tns:sayHiResponse">
        
</part>
    
</message>
    
<portType name="Hello">
        
<operation name="sayHi">
            
<input message="tns:sayHi">
            
</input>
            
<output message="tns:sayHiResponse">
            
</output>
        
</operation>
    
</portType>
    
<binding name="HelloPortBinding" type="tns:Hello">
        
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document">
        
</soap:binding>
        
<operation name="sayHi">
            
<soap:operation soapAction="">
            
</soap:operation>
            
<input>
                
<soap:body use="literal">
                
</soap:body>
            
</input>
            
<output>
                
<soap:body use="literal">
                
</soap:body>
            
</output>
        
</operation>
    
</binding>
    
<service name="HelloService">
        
<port name="HelloPort" binding="tns:HelloPortBinding">
            
<soap:address location="http://localhost:8080/WebServiceApp/HelloService">
            
</soap:address>
        
</port>
    
</service>
</definitions>


也可以編寫客戶端測試,新建一個普通的java項目,在項目上右鍵,選擇新建--Web服務客戶端,在彈出窗口中指定WebService項目或者WSDL url,點擊完成。在源代碼上右鍵,選擇Web服務客戶端資源--調用Web服務操作,在彈出窗口中選擇sayHi操作,點確定,測試代碼自動生成:

public class Main {

    
public static void main(String[] args) {

        
try 

            webservice.HelloService service 
= new webservice.HelloService();
            webservice.Hello port 
= service.getHelloPort();
            
            java.lang.String name 
= "Tom";
            java.lang.String result 
= port.sayHi(name);
            System.out.println(
"Result = " + result);
        }
 catch (Exception ex) {
            
// TODO handle custom exceptions here
        }

    }

}

運行該客戶端,結果將會輸出

waynemao 2008-09-13 02:31 發表評論
]]>
oracle視頻http://www.aygfsteel.com/waynemao/archive/2008/09/13/228688.htmlwaynemaowaynemaoFri, 12 Sep 2008 17:01:00 GMThttp://www.aygfsteel.com/waynemao/archive/2008/09/13/228688.htmlhttp://www.aygfsteel.com/waynemao/comments/228688.htmlhttp://www.aygfsteel.com/waynemao/archive/2008/09/13/228688.html#Feedback0http://www.aygfsteel.com/waynemao/comments/commentRss/228688.htmlhttp://www.aygfsteel.com/waynemao/services/trackbacks/228688.htmloracle10g_1

oracle10g_2

oracle10g_3_1

oracle10g_3_2

oracle10g_4_1

oracle10g_4_2

oracle10g_5_1

oracle10g_5_2

oracle10g_6

oracle10g_7_1

oracle10g_7_2

oracle10g_8

oracle10g_9

oracle10g_10

oracle10g_11

oracle10g_12

oracle10g_13

oracle10g_14



waynemao 2008-09-13 01:01 發表評論
]]>
EJB3 (5)http://www.aygfsteel.com/waynemao/archive/2008/09/13/228685.htmlwaynemaowaynemaoFri, 12 Sep 2008 16:42:00 GMThttp://www.aygfsteel.com/waynemao/archive/2008/09/13/228685.htmlhttp://www.aygfsteel.com/waynemao/comments/228685.htmlhttp://www.aygfsteel.com/waynemao/archive/2008/09/13/228685.html#Feedback0http://www.aygfsteel.com/waynemao/comments/commentRss/228685.htmlhttp://www.aygfsteel.com/waynemao/services/trackbacks/228685.html同一個會話bean也可以實現多個遠程接口,不過代碼上有些地方要注意,首先寫第一個接口:

@Remote
public interface HelloRemote {

    String sayHi(String name);
}

第二個接口:
@Remote
public interface HelloRemote1 {
    
    String sayBye(String name);
}

接下來寫會話bean,同時實現以上兩個接口:
@Stateless(mappedName="hello")
public class HelloBean implements HelloRemote, HelloRemote1 {

    
public String sayHi(String name) {
        
return "Hi " + name; 
    }


    
public String sayBye(String name) {
        
return "Bye " + name;
    }
    
}

注意這里用到了mappedName元素,這個很關鍵。把EJB模塊打包部署,接下來在遠程客戶端測試,先寫Servlet:
    @EJB(mappedName="hello#ejb.HelloRemote1")
    
private HelloRemote1 helloBean1;
    @EJB(mappedName
="hello#ejb.HelloRemote")
    
private HelloRemote helloBean;

注意@EJB標注里也使用了mappedName元素,值的樣式是:JNDI名#包名.接口名。再看Jsp:
<%
            InitialContext ctx 
= new InitialContext();
            HelloRemote helloBean 
= (HelloRemote)ctx.lookup("hello#ejb.HelloRemote");
            out.println(helloBean.sayHi(
"Tom"));
            HelloRemote1 helloBean1 
= (HelloRemote1)ctx.lookup("hello#ejb.HelloRemote1");
            out.println(
"<br>" + helloBean1.sayBye("Tom"));
%>

和Servlet中的一樣

waynemao 2008-09-13 00:42 發表評論
]]>
EJB3 (4)http://www.aygfsteel.com/waynemao/archive/2008/09/13/228680.htmlwaynemaowaynemaoFri, 12 Sep 2008 16:21:00 GMThttp://www.aygfsteel.com/waynemao/archive/2008/09/13/228680.htmlhttp://www.aygfsteel.com/waynemao/comments/228680.htmlhttp://www.aygfsteel.com/waynemao/archive/2008/09/13/228680.html#Feedback0http://www.aygfsteel.com/waynemao/comments/commentRss/228680.htmlhttp://www.aygfsteel.com/waynemao/services/trackbacks/228680.html如果一個遠程接口有兩個實現,需要用mappedName來區分
首先定義一個遠程接口:

@Remote
public interface HelloRemote {

    String sayHi(String name);
}


第一個實現:
@Stateless(mappedName="hello")
public class HelloBean implements HelloRemote {

    
public String sayHi(String name) {
        
return "Hi " + name; 
    }

}

第二個實現:
@Stateless(mappedName="hello2")
public class HelloBean2 implements HelloRemote{

    
public String sayHi(String name) {
        
return "Hello " + name;
    }


}

兩個無狀態會話bean實現了同一個遠程接口,但它們的mappedName不一樣,還有它們各自重寫了sayHi業務方法。部署EJB模塊然后測試,首先是Servlet:
    @EJB(mappedName="hello2")
    
private HelloRemote helloBean2;
    @EJB(mappedName
="hello")
    
private HelloRemote helloBean;

然后是JSP:
<%
            InitialContext ctx 
= new InitialContext();
            HelloRemote helloBean 
= (HelloRemote)ctx.lookup("hello");
            out.println(helloBean.sayHi(
"Tom"));
            HelloRemote helloBean2 
= (HelloRemote)ctx.lookup("hello2");
            out.println(
"<br>" + helloBean2.sayHi("Tom"));
%>

呵呵,遠程調用也實現了多態

waynemao 2008-09-13 00:21 發表評論
]]>
EJB3 (3)http://www.aygfsteel.com/waynemao/archive/2008/09/12/228669.htmlwaynemaowaynemaoFri, 12 Sep 2008 14:24:00 GMThttp://www.aygfsteel.com/waynemao/archive/2008/09/12/228669.htmlhttp://www.aygfsteel.com/waynemao/comments/228669.htmlhttp://www.aygfsteel.com/waynemao/archive/2008/09/12/228669.html#Feedback0http://www.aygfsteel.com/waynemao/comments/commentRss/228669.htmlhttp://www.aygfsteel.com/waynemao/services/trackbacks/228669.html
@Local
public interface MessageLocal {

    String getMessage();
    
}

接著編寫一個會話bean實現該接口:
@Stateless
public class MessageBean implements MessageLocal {

    
public String getMessage() {
        
return "Hello world";
    }

    
}

然后創建一個遠程接口:

@Remote
public interface HelloRemote {

    String welcome();
    
}

編寫一個會話bean實現該接口:
@Stateless
public class HelloBean implements HelloRemote {
    @EJB
    
private MessageLocal messageBean;

    
public String welcome() {
        
return messageBean.getMessage();
    }

    
}

在遠程對象里聲明了一個本地接口的引用,并嘗試在遠程方法當中調用本地接口的本地方法。這些都沒問題,打包部署成功。
<%
            InitialContext ctx 
= new InitialContext();
            HelloRemote helloBean 
= (HelloRemote)ctx.lookup(HelloRemote.class.getName());
            out.println(helloBean.welcome());
%>

在遠程客戶端的代碼如此,看起來一切正常,不過在部署WEB模塊的時候報異常:正在域中部署應用程序 失敗;為模塊 [EjbWebClient] 裝入部署描述符時出錯 -- Cannot resolve reference Unresolved Ejb-Ref ejb.HelloBean/messageBean@jndi: @null@ejb.MessageLocal@Session@null


waynemao 2008-09-12 22:24 發表評論
]]>
EJB3 (2)http://www.aygfsteel.com/waynemao/archive/2008/09/12/228658.htmlwaynemaowaynemaoFri, 12 Sep 2008 13:29:00 GMThttp://www.aygfsteel.com/waynemao/archive/2008/09/12/228658.htmlhttp://www.aygfsteel.com/waynemao/comments/228658.htmlhttp://www.aygfsteel.com/waynemao/archive/2008/09/12/228658.html#Feedback0http://www.aygfsteel.com/waynemao/comments/commentRss/228658.htmlhttp://www.aygfsteel.com/waynemao/services/trackbacks/228658.html一個會話bean即可以遠程訪問,也可以本地訪問,盡管這種現象不常見。
我們先定義遠程接口:

@Remote
public interface HelloRemote {

    String sayHi(String name);

    String sayByeBye(String name);
    
}

接著定義本地接口:
@Local
public interface HelloLocal {

    String sayBye(String name);
    
}

然后編寫一個會話bean同時實現兩個接口:
@Stateless
public class HelloBean implements HelloRemote, HelloLocal {

    
public String sayHi(String name) {
        
return "Hi " + name;
    }

    
    
public String sayBye(String name) {
        
return "Bye " + name;
    }

    
    
public String sayByeBye(String name) {
        
return sayBye(name);
    }

    
}

那么,這還是一個無狀態會話bean,不過即可以遠程訪問,也可以本地訪問。
<%
            InitialContext ctx 
= new InitialContext();
            HelloRemote helloBean 
= (HelloRemote)ctx.lookup(HelloRemote.class.getName());
            out.println(helloBean.sayHi(
"Tom"));
            out.println(
"<br>" + helloBean.sayByeBye("Tom"));
%>

遠程訪問只能獲得遠程接口的代理,然后調用遠程業務方法。在遠程客戶端是無法獲得本地接口的引用,更別說訪問本地方法了。

waynemao 2008-09-12 21:29 發表評論
]]>
主站蜘蛛池模板: 合水县| 迁西县| 巩留县| 临夏县| 天气| 平凉市| 云龙县| 无锡市| 姚安县| 乡城县| 营口市| 永定县| 平邑县| 贡山| 准格尔旗| 西乡县| 镇原县| 毕节市| 台湾省| 驻马店市| 巨野县| 师宗县| 眉山市| 黑水县| 平度市| 卫辉市| 休宁县| 澜沧| 松潘县| 都昌县| 襄垣县| 安塞县| 青岛市| 阿巴嘎旗| 特克斯县| 两当县| 翼城县| 武胜县| 和平区| 中牟县| 淮滨县|