# 插件开发说明
插件开发手册
## **一、说明**
在实际项目应用当中,YFE前置系统内置的报文组件可能满足不了某些报文解析要求,此时项目实施人员可以开发流程插件对特殊报文进行处理;在前置流程收到报文后调用插件进行解包,在返回应答报文前调用插件进行组包报文
## **二、插件开发**
插件开发本质上是编写一个业务处理jar包,放置到指定位置,然后在流程配置中配置对应的类路径即可使用。
### 1、新建Java项目
新建一个普通的Java project即可。
如果新建的是maven项目,要注意最后打包jar不能是运行包,maven依赖三方库注意单独拿出来,方面后续放到plugin目录
建议使用普通Java project
如果有之前创建的插件项目,亦可使用
### 2、编写业务处理逻辑
在项目新建一个Java class。类名按业务自行定义。类的结构如下。
```
<pre class="calibre29">```
package com<span class="token1">.</span>ylink<span class="token1">.</span>plugin<span class="token1">.</span>demo<span class="token1">;</span>
import java<span class="token1">.</span>util<span class="token1">.</span>HashMap<span class="token1">;</span>
import java<span class="token1">.</span>util<span class="token1">.</span>Map<span class="token1">;</span>
import org<span class="token1">.</span>slf4j<span class="token1">.</span>Logger<span class="token1">;</span>
import cn<span class="token1">.</span>hutool<span class="token1">.</span>core<span class="token1">.</span>date<span class="token1">.</span>DateUtil<span class="token1">;</span>
public class <span class="token5">DemoPlugin</span> <span class="token1">{</span>
<span class="token6">/*
* 日志对象,由通讯服务自动注入
*/</span>
public static Logger log<span class="token1">;</span>
<span class="token6">/**
* 初始化方法
*
* @param cfgs 当前接入配置
*/</span>
public static void <span class="token5">initEnv</span><span class="token1">(</span>Map cfgs<span class="token1">)</span> <span class="token1">{</span>
<span class="token1">}</span>
<span class="token6">/**
* 插件具体执行业务处理 <br/>
* 方法必须是static的 <br/>
* 方法的入参的顺序和数量是固定的<br/>
*
* @param tranCode 交易码
* @param inObj 传入参数。有流程配置时指定
* @return 返回值无约束,根据业务执行定义
* @throws Exception
*/</span>
public static Map<span class="token"><</span>String<span class="token1">,</span> Object<span class="token">></span> <span class="token5">test</span><span class="token1">(</span>String tranCode<span class="token1">,</span> Object inObj<span class="token1">)</span> throws Exception <span class="token1">{</span>
log<span class="token1">.</span><span class="token5">info</span><span class="token1">(</span><span class="token3">"客户化插件调用开始。。。。。。。"</span><span class="token1">)</span><span class="token1">;</span>
log<span class="token1">.</span><span class="token5">info</span><span class="token1">(</span><span class="token3">"参数 1 交易码={}"</span><span class="token1">,</span> tranCode<span class="token1">)</span><span class="token1">;</span>
log<span class="token1">.</span><span class="token5">info</span><span class="token1">(</span><span class="token3">"参数 2 配置参数=={}"</span><span class="token1">,</span> inObj<span class="token1">)</span><span class="token1">;</span>
<span class="token6">// TODO 1、参数校验</span>
<span class="token6">// TODO 2、业务处理</span>
Map<span class="token"><</span>String<span class="token1">,</span> Object<span class="token">></span> retMap <span class="token">=</span> <span class="token4">new</span> <span class="token5">HashMap</span><span class="token"><</span>String<span class="token1">,</span> Object<span class="token">></span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span>
retMap<span class="token1">.</span><span class="token5">put</span><span class="token1">(</span><span class="token3">"tranCode"</span><span class="token1">,</span> tranCode<span class="token1">)</span><span class="token1">;</span>
retMap<span class="token1">.</span><span class="token5">put</span><span class="token1">(</span><span class="token3">"date"</span><span class="token1">,</span> DateUtil<span class="token1">.</span><span class="token5">now</span><span class="token1">(</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span>
<span class="token4">return</span> retMap<span class="token1">;</span>
<span class="token1">}</span>
<span class="token1">}</span>
```
```
- log 日志对象,固定名称,不可改,为org.slf4j.Logger 对象
- 类内为静态方法
- 方法的入参是固定的格式,不能修改
- 方法返回值可以根据业务自行定义
- 插件中使用到三方库,直接放到项目的lib文件夹,设置classpth即可
### 3、导出jar
Eclipse中直接在项目中右键 export Jar file即可:
- 导出类型选择 Java-> JAR file
![](https://img.kancloud.cn/63/7c/637cbe682a1a258acc93e26968f1b977_1006x606.png)
- 导出文件只选择src即可。Lib目录无需导出
![](https://img.kancloud.cn/53/ca/53ca6414e5f3f78e540e81feb7f3fa50_1016x188.png)
## **三、部署、配置**
### 1、部署
将导出的jar放到通讯服务的根目录的plugin文件夹(如没有,新建一个):
![](https://img.kancloud.cn/a9/44/a944d1e9f37582faa4040b91c891003d_976x526.png)
- plugin文件夹和项目运行包同级
- 如果插件中使用的三方库(插件项目lib目录jar)在通讯服务中没有引入,需要将三方jar也放到plugin目录下
### 2、配置
流程接入配置:根据业务实际流程在相应位置增加 【插件调用】节点。
![](https://img.kancloud.cn/b6/c4/b6c4ccaaa408b67ef981a470b8ab309a_528x346.png)
配置插件信息:类名+方法名
![](https://img.kancloud.cn/d1/f2/d1f21e03123f209d2d542915d964f137_1107x603.png)
- 类名必须是类的全路径名称,通讯服务根据类名反射获取类实例
- 方法名必须和插件类一致,通讯按照以下规则获取方法调用:getMethod(strMethod, String.class, Object.class)