热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Axis2发布webservice(4)—利用XML文件同时发布多个webservice和跨多个WebService管理Session...

我们需要ServiceGroupContext保存跨越多个webservice的session信息;同时需要设置services.xml文件的中service的sco

我们需要ServiceGroupContext保存跨越多个webservice的session信息;同时需要设置services.xml文件的中service的scope属性为application

一、编写两个webservice:

LoginServiceApplication.java代码如下:

package com.hoo.service;import org.apache.axis2.context.MessageContext;
import org.apache.axis2.context.ServiceGroupContext;public class LoginServiceApplication {//登陆方法public boolean login(String userName,String password){MessageContext context = MessageContext.getCurrentMessageContext();//跨越多个WebService管理session第一步是实例化一个ServiceGroupContext对象,而不是ServiceContextServiceGroupContext ctx = context.getServiceGroupContext();if("admin".equals(userName)&&"123456".equals(password)){ctx.setProperty("username", userName);ctx.setProperty("password", password);ctx.setProperty("msg","登陆成功");return true;}else{ctx.setProperty("msg", "登陆失败");return false;}}public String getLoginMessage(){MessageContext context = MessageContext.getCurrentMessageContext();ServiceGroupContext ctx = context.getServiceGroupContext();String msg = ctx.getProperty("username")+","+ctx.getProperty("msg");return msg;}}

 

SearchSessionService.java代码如下:

package com.hoo.service;import org.apache.axis2.context.MessageContext;
import org.apache.axis2.context.ServiceGroupContext;public class SearchSessionService {public String findSessionMessage(String key){MessageContext context &#61; MessageContext.getCurrentMessageContext();ServiceGroupContext ctx &#61; context.getServiceGroupContext();if(ctx.getProperty(key)!&#61;null){return "找到session&#xff1a;" &#43; key &#43; "," &#43;ctx.getProperty(key);}else{return "没有找到<" &#43; key &#43; ">的数据";}}
}

 

上面两个webservice都用到了ServiceGroupContext的对象。

 

二、 编写services.xml&#xff0c;同时发布2个webservice&#xff0c;并且将scope设为application&#xff0c;services.xml的内容如下&#xff1a;

<serviceGroup><service name&#61;"LoginSessionService" scope&#61;"application"><description>Web Service Session例子description><parameter name&#61;"ServiceClass">com.hoo.service.LoginServiceApplicationparameter><messageReceivers><messageReceiver mep&#61;"http://www.w3.org/2004/08/wsdl/in-out"class&#61;"org.apache.axis2.rpc.receivers.RPCMessageReceiver" /><messageReceiver mep&#61;"http://www.w3.org/2004/08/wsdl/in-only"class&#61;"org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />messageReceivers>service><service name&#61;"SearchSessionService" scope&#61;"application"><description>Web Service Search Session例子description><parameter name&#61;"ServiceClass">com.hoo.service.SearchSessionService parameter><messageReceivers><messageReceiver mep&#61;"http://www.w3.org/2004/08/wsdl/in-out"class&#61;"org.apache.axis2.rpc.receivers.RPCMessageReceiver" /><messageReceiver mep&#61;"http://www.w3.org/2004/08/wsdl/in-only"class&#61;"org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />messageReceivers>service>
serviceGroup>

 

在eclipse里面&#xff0c;file->new->other->axis2 service archiver->next

选择webservice的class文件存放路径&#xff0c;一般选到bin文件夹就行了

然后&#xff0c;next->next->next&#xff0c;出现选择XML的界面&#xff1a;

1

 

取消勾选“generate the service XML automatically”&#xff0c;在上面的文件浏览框中选中刚才保存的services.xml文件&#xff0c;next

选择输出路径和文件名&#xff08;这里的文件名和webservice的名字没有关系&#xff0c;webservice的名字在XML文件中定义&#xff09;&#xff0c;next->finish,出现创建成功的提示即可

然后利用前面教程中的方法将此AAR文件发不成webservice即可

 

三、编写客户端代码&#xff1a;

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 LoginSessionServiceClient {public static void main(String[] args) throws AxisFault {String target &#61; "http://localhost:8080/axis2/services/LoginSessionService";RPCServiceClient client &#61; new RPCServiceClient();Options options &#61; client.getOptions();//打开对session的支持options.setManageSession(true);EndpointReference epr &#61; new EndpointReference(target);options.setTo(epr);QName qname &#61; new QName("http://service.hoo.com", "login");//指定调用的方法和传递参数数据&#xff0c;及设置返回值的类型Object[] result &#61; client.invokeBlocking(qname, new Object[] { "admin", "123456" }, new Class[] { boolean.class });System.out.println(result[0]);qname &#61; new QName("http://service.hoo.com", "getLoginMessage");result &#61; client.invokeBlocking(qname, new Object[] { null }, new Class[] { String.class });System.out.println(result[0]);target &#61; "http://localhost:8080/axis2/services/SearchSessionService";epr &#61; new EndpointReference(target);options.setTo(epr);qname &#61; new QName("http://service.hoo.com", "findSessionMessage");result &#61; client.invokeBlocking(qname, new Object[] { "username" }, new Class[] { String.class });System.out.println(result[0]);qname &#61; new QName("http://service.hoo.com", "findSessionMessage");result &#61; client.invokeBlocking(qname, new Object[] { "msg" }, new Class[] { String.class });System.out.println(result[0]);qname &#61; new QName("http://service.hoo.com", "findSessionMessage");result &#61; client.invokeBlocking(qname, new Object[] { "password" }, new Class[] { String.class });System.out.println(result[0]);}
}

运行后结果如下&#xff1a;

true
      admin,登陆成功
      找到session&#xff1a;username,admin
      找到session&#xff1a;msg,登陆成功
      找到session&#xff1a;password,123456

 

如果将services.xml文件"SearchSessionService" scope&#61;"application">的内容改成scope&#61;transportsession&#xff0c;就会出现找不到session中的内容错误。

转:https://www.cnblogs.com/hewenwu/p/3862991.html



推荐阅读
  • Android系统移植与调试之如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
    本文介绍了如何修改Android设备状态条上音量加减键在横竖屏切换时的显示与隐藏。通过修改系统文件system_bar.xml实现了该功能,并分享了解决思路和经验。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • r2dbc配置多数据源
    R2dbc配置多数据源问题根据官网配置r2dbc连接mysql多数据源所遇到的问题pom配置可以参考官网,不过我这样配置会报错我并没有这样配置将以下内容添加到pom.xml文件d ... [详细]
  • YOLOv7基于自己的数据集从零构建模型完整训练、推理计算超详细教程
    本文介绍了关于人工智能、神经网络和深度学习的知识点,并提供了YOLOv7基于自己的数据集从零构建模型完整训练、推理计算的详细教程。文章还提到了郑州最低生活保障的话题。对于从事目标检测任务的人来说,YOLO是一个熟悉的模型。文章还提到了yolov4和yolov6的相关内容,以及选择模型的优化思路。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
author-avatar
手机用户2602915671
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有