wso2esb 如何在可执行文件 class mediator jar 中添加所有依赖的 jar 文件

wso2esb how can add all dependent jar file in a executable class mediator jar

我使用 WSO2 Developer Studio 创建了项目。在 src/main/java 下,我创建了包 com.training 在那个包中 class UnzipFileMediator:

package com.training.mediators;
import org.apache.synapse.MessageContext; 
import org.apache.synapse.mediators.AbstractMediator;

//Import another jar dependent jar
import com.test.Testing;


public class UnzipFileMediator extends AbstractMediator { 

    public boolean mediate(MessageContext context) { 
        // TODO Implement your mediation logic here 
        System.out.println("UnzipFile Mediation entered*****");

       // Dependent JAR class

        Testing t= new Testing();
        t.sayHello();
        return true;
    }
}

在这个项目中,我使用以下步骤部署了另一个 jar

1. Right click on the project
2. Select configure build path
3. Select Java Build Path
4. Select Libraries
5.Click on Add External Jar. Then add the external jar called Hello
6. Hello Jar contain the class Testing
7. Above mentioned class import Dependent Testing class and create the `object and call the function as mentioned below.`
8.import com.test.Testing;
9. Its working fine here.

现在我要将它部署到 WSO2ESB 上,以下是创建 Jar 文件的步骤:

1. Right click on the project
2. Select Export
3. Select Runnable JAR File
4. Select the check box 
                Package Required libraries into generated JAR
6. JAR is created and available to deploy at WSO2ESB

我会将这个 jar 部署到 WSO2ESB 下面是位置:

1. <ESB_HOME>/repository/components/lib directory.

在突触配置中,我这样调用 class

<class name="samples.mediators.UnzipFileMediator"></class>

但是当我尝试调用 it.It 时生成以下错误:

java.lang.NoClassDefFoundError: com/test/Testing

这是我上面提到的依赖 JAR 文件class

请指导我做错了什么

您可以简单地将依赖的jar 放在lib 目录中。但要确保两个或多个 jar 不包含相同的包(如果发生这种情况,class 会出现加载问题)

我建议,如果您可以制作 maven OSGi 包,这将非常容易。我将解释如何去做。

第 1 步: 创建一个 maven-archetype-quickstart projet.You 可以使用这个生成它 command.If 你已经安装了 Maven。

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

然后根据需要创建一个包。作为例子,它应该与此类似。

第 2 步:

然后你需要修改你pom.xml类似这样。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<artifactId>com.training.mediators</artifactId>
<packaging>bundle</packaging>
<groupId>4.0.0</groupId>
<name>com.training.mediators</name>
<url>http://maven.apache.org</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.apache.synapse</groupId>
        <artifactId>synapse-core</artifactId>
        <version>2.1.3-wso2v11</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.felix</groupId>
            <artifactId>maven-bundle-plugin</artifactId>
            <version>1.4.0</version>
            <extensions>true</extensions>
            <configuration>
                <instructions>
                    <Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
                    <Bundle-Name>${project.artifactId}</Bundle-Name>
                    <Export-Package>
                        com.training.*
                    </Export-Package>
                    <Import-Package>
                        *,*;resolution:=optional
                    </Import-Package>
                    <Fragment-Host>synapse-core</Fragment-Host>
                </instructions>
            </configuration>
        </plugin>
    </plugins>
</build>


<repositories>
    <repository>
        <id>wso2-maven2-repository</id>
        <url>http://dist.wso2.org/maven2</url>
    </repository>
    <repository>
        <id>apache-Incubating-repo</id>
        <name>Maven Incubating Repository</name>
        <url>http://people.apache.org/repo/m2-incubating-repository</url>
    </repository>
    <repository>
        <id>apache-maven2-repo</id>
        <name>Apache Maven2 Repository</name>
        <url>http://repo1.maven.org/maven2/</url>
    </repository>
    <repository>
        <id>wso2-nexus</id>
        <name>WSO2 internal Repository</name>
        <url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
        <releases>
            <enabled>true</enabled>
            <updatePolicy>daily</updatePolicy>
            <checksumPolicy>ignore</checksumPolicy>
        </releases>
    </repository>
</repositories>


<pluginRepositories>
    <pluginRepository>
        <id>wso2-maven2-repository</id>
        <url>http://dist.wso2.org/maven2</url>
    </pluginRepository>
    <pluginRepository>
        <id>wso2-maven2-snapshot-repository</id>
        <url>http://dist.wso2.org/snapshots/maven2</url>
    </pluginRepository>
</pluginRepositories>

这里有一点很重要,就是export-package部分,你需要说明要导出哪些子包。

                    <Export-Package>
                        com.training.*
                    </Export-Package>

您不需要导入依赖项。这些东西会自动导入。

第 3 步:

现在是时候在调解器中写入您的逻辑了class。

package com.training.mediators;

import org.apache.synapse.MessageContext;
import org.apache.synapse.mediators.AbstractMediator;

public class UnzipFileMediator extends AbstractMediator {

 public boolean mediate(MessageContext context) {
    // TODO Implement your mediation logic here
    System.out.println("UnzipFile Mediation entered*****");

    // Dependent JAR class

    return true;
}

public String getType() {
    return null;
}

public void setTraceState(int traceState) {
    traceState = 0;
}

 public int getTraceState() {
     return 0;
 }
}

第 4 步:

在 pom.xml 文件所在的目录中键入 mvn clean package

然后会生成一个名为target的文件夹。在该文件夹中,应该有 *.jar 类似于以下内容的文件:com.training.mediators-1.0-SNAPSHOT.jar

com.training.mediators-1.0-SNAPSHOT.jar复制到wso2esb-4.9.0/repository/components/dropins

重新启动 ESB 服务器。

第 5 步:

现在您需要根据需要添加 ESB 配置。这是示例

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://ws.apache.org/ns/synapse">
<sequence name="fault">
    <makefault version="soap11">
        <code value="tns:Receiver" xmlns:tns="http://www.w3.org/2003/05/soap-envelope"/>
        <reason value="Mediation failed."/>
    </makefault>
    <send/>
</sequence>
<sequence name="main" onError="fault">
    <in>
        <class name="com.training.mediators.UnzipFileMediator"/>
        <send>
            <endpoint name="stockquote">
                <address uri="http://localhost:9000/services/AdderService?wsdl"/>
            </endpoint>
        </send>
    </in>
    <out>
        <send/>
    </out>
</sequence>

希望对您有所帮助:)