當多個WebService的時候,我們要管理它的Session。這個時候我們得依靠ServiceGroupContext保存session信息;然后在發布WebService的時候,services.xml文件的的service表情的scope就不再說request或是transportsession了,而是application;最后同樣要開啟對session的管理,即options.setManageSession(
true
);
代碼
package
com.hoo.service;
import
org.apache.axis2.context.MessageContext;
import
org.apache.axis2.context.ServiceGroupContext;
/** * <b>function:</b>管理多個會話Session信息 * @author hoojo * @createDate 2011-3-9 下午05:11:07 * @file LoginSessionService.java * @package com.hoo.service * @project Axis2WebService * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */
public
class
LoginSessionService {
public
boolean
login(String userName, String password) {
MessageContext context = MessageContext.getCurrentMessageContext();
ServiceGroupContext ctx = context.getServiceGroupContext();
if
("
admin
".equals(userName) && "
123456
".equals(password)) {
ctx.setProperty("
userName
", userName);
ctx.setProperty("
password
", password);
ctx.setProperty("
msg
", "
登陸成功
");
return
true
;
}
ctx.setProperty("
msg
", "
登陸失敗
");
return
false
;
}
public
String getLoginMessage() {
MessageContext context = MessageContext.getCurrentMessageContext();
ServiceGroupContext ctx = context.getServiceGroupContext();
return
ctx.getProperty("
userName
") + "
#
" + ctx.getProperty("
msg
");
}
}
代碼
package
com.hoo.service;
import
org.apache.axis2.context.MessageContext;
import
org.apache.axis2.context.ServiceGroupContext;
/** * <b>function:</b>查找多服務Session會話中的消息 * @author hoojo * @createDate 2011-3-9 下午05:22:39 * @file SearchSessionServcie.java * @package com.hoo.service * @project Axis2WebService * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */
public
class
SearchSessionServcie {
public
String findSessionMessage(String key) {
MessageContext mc = MessageContext.getCurrentMessageContext();
ServiceGroupContext ctx = mc.getServiceGroupContext();
if
(ctx.getProperty(key) !=
null
) {
return
"
找到的數據<
" + key + "
,
" + ctx.getProperty(key) + "
>
";
}
else
{
return
"
沒有找到<
" + key + "
>的數據
";
}
}
}
2、 編寫services.xml來發布這2個服務,還以前不一樣的。這一次是用一個services.xml文件配置2個service,同時發布2個服務。Xml代碼如下:
代碼
<
serviceGroup
>
<
service
name
=
"LoginSessionService"
scope
=
"application"
>
<
description
>
Web Service Session例子
</
description
>
<
parameter
name
=
"ServiceClass"
>
com.hoo.service.LoginSessionService
</
parameter
>
<
messageReceivers
>
<
messageReceiver
mep
=
"http://www.w3.org/2004/08/wsdl/in-out"
class
=
"org.apache.axis2.rpc.receivers.RPCMessageReceiver"
/>
<
messageReceiver
mep
=
"http://www.w3.org/2004/08/wsdl/in-only"
class
=
"org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"
/>
</
messageReceivers
>
</
service
>
<
service
name
=
"SearchSessionService"
scope
=
"application"
>
<
description
>
Web Service Search Session例子
</
description
>
<
parameter
name
=
"ServiceClass"
>
com.hoo.service.SearchSessionServcie
</
parameter
>
<
messageReceivers
>
<
messageReceiver
mep
=
"http://www.w3.org/2004/08/wsdl/in-out"
class
=
"org.apache.axis2.rpc.receivers.RPCMessageReceiver"
/>
<
messageReceiver
mep
=
"http://www.w3.org/2004/08/wsdl/in-only"
class
=
"org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"
/>
</
messageReceivers
>
</
service
>
</
serviceGroup
>
代碼
package
com.hoo.service;
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;
/** * <b>function:</b>多會話Session管理,WebService客戶端請求代碼 * @author hoojo * @createDate 2011-3-9 下午05:17:15 * @file LoginSessionServiceClient.java * @package com.hoo.service * @project Axis2WebService * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */
public
class
LoginSessionServiceClient {
public
static
void
main(String[] args)
throws
AxisFault {
String target = "
http://localhost:8080/axis2/services/LoginSessionService
";
RPCServiceClient client =
new
RPCServiceClient();
Options options = client.getOptions();
options.setManageSession(
true
);
EndpointReference epr =
new
EndpointReference(target);
options.setTo(epr);
QName qname =
new
QName("
http://service.hoo.com
", "
login
");
//指定調用的方法和傳遞參數數據,及設置返回值的類型
Object[] result = client.invokeBlocking(qname,
new
Object[] { "
admin
", "
123456
" },
new
Class[] {
boolean
.
class
});
System.out.println(result[0]);
qname =
new
QName("
http://service.hoo.com
", "
getLoginMessage
");
result = client.invokeBlocking(qname,
new
Object[] {
null
},
new
Class[] { String.
class
});
System.out.println(result[0]);
target = "
http://localhost:8080/axis2/services/SearchSessionService
";
epr =
new
EndpointReference(target);
options.setTo(epr);
qname =
new
QName("
http://service.hoo.com
", "
findSessionMessage
");
result = client.invokeBlocking(qname,
new
Object[] { "
userName
" },
new
Class[] { String.
class
});
System.out.println(result[0]);
qname =
new
QName("
http://service.hoo.com
", "
findSessionMessage
");
result = client.invokeBlocking(qname,
new
Object[] { "
msg
" },
new
Class[] { String.
class
});
System.out.println(result[0]);
qname =
new
QName("
http://service.hoo.com
", "
findSessionMessage
");
result = client.invokeBlocking(qname,
new
Object[] { "
password
" },
new
Class[] { String.
class
});
System.out.println(result[0]);
}
}