其他分享
首页 > 其他分享> > pf4j 试用

pf4j 试用

作者:互联网

pf4j 试用上还是比较灵活的,并没有太多的配置,而且比较灵活,支持类隔离

参考项目

├── README.md
├── bootstrap  // 启动入口,使用了assembly 进行打包,当然对图spring 项目也是可以的
│   ├── pom.xml
│   └── src
│       ├── main
│       │   ├──assembly
│       │   ├── java
│       │   └── resources
│       └── test
│           └── java
├── loginpluginb  // 插件1
│   ├── pom.xml
│   └── src
│       ├── main
│       │   ├── java
│       │   └── resources
│       └── test
│           └── java
├── loginpluginc // 插件2
│   ├── pom.xml
│   └── src
│       ├── main
│       │   ├── java
│       │   └── resources
│       └── test
│           └── java
├── pom.xml
├── service-contract // 插件服务契约
│   ├── pom.xml
│   └── src
│       ├── main
│       │   ├── java
│       │   └── resources
│       └── test
│           └── java
└── src
    ├── main
    │   ├── java
    │   └── resources
    └── test
        └── java
 
package com.dalong;
 
import org.pf4j.ExtensionPoint;
 
/**
 * @author dalong
 * userlogin service contract
 */
public interface UserLogin extends  ExtensionPoint {
    /**
     * userlogin service contract
     * @param name name
     * @param password password
     * @return token
     */
    String token(String name,String password);
}

插件实现(继承Plugin)进行扩展,添加注解@Extension
实现Plugin的目的是进行生命周期的控制

 
package com.dalong;
 
import org.pf4j.Extension;
import org.pf4j.Plugin;
import org.pf4j.PluginWrapper;
 
/**
 * login plugin c
 */
public class MyLoginPluginC extends Plugin {
    public MyLoginPluginC(PluginWrapper wrapper) {
        super(wrapper);
    }
 
    @Override
    public void delete() {
        super.delete();
        System.out.println("pluginc  delete");
    }
 
    @Override
    public void stop() {
        super.stop();
        System.out.println("pluginc  stop");
 
    }
 
    @Override
    public void start() {
        super.start();
        System.out.println("pluginc  start");
 
    }
    @Extension
    public  static  class MyLoginC implements  UserLogin {
 
        @Override
        public String token(String name, String password) {
            return String.format("%s-%s-plugin c",name,password);
        }
    }
}

插件打包说明
默认插件的加载包含了classpath 以及serviceloader(spi,但是默认没开启)可以基于jar 的manifest以及plugin.properties
基于jar 模式比较好,而且比较标准

 
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>pf4j-learning</artifactId>
        <groupId>com.dalong</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>loginpluginc</artifactId>
 
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <plugin.id>loginpluginc</plugin.id>
        <plugin.class>com.dalong.MyLoginPluginC</plugin.class>
        <plugin.version>0.0.1</plugin.version>
        <plugin.provider>dalong</plugin.provider>
        <plugin.dependencies />
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>com.dalong</groupId>
            <artifactId>service-contract</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.pf4j</groupId>
            <artifactId>pf4j</artifactId>
            <version>3.6.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                // manifest 维护
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Plugin-Id>${plugin.id}</Plugin-Id>
                            <Plugin-Class>${plugin.class}</Plugin-Class>
                            <Plugin-Version>${plugin.version}</Plugin-Version>
                            <Plugin-Provider>${plugin.provider}</Plugin-Provider>
                            <Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

bootstap 入口,比较简单,包含了支持serviceloader 的以及自定义classpath的
servieloader模式的

 

 

 
public class MyDefaultLogin implements  UserLogin{
    @Override
    public String token(String name, String password) {
        return String.format("%s-%s-default ",name,password);
    }
}
 

自定义的

package com.dalong;
 
import org.pf4j.Extension;
 
@Extension
public class Pf4JLogin implements  UserLogin{
    @Override
    public String token(String name, String password) {
        return String.format("%s-%s-Pf4JLogin ",name,password);
    }
}
 

入口打包(方法很多,可以使用shared 以及maven-assembly-plugin)
maven pom.xml


 
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>pf4j-learning</artifactId>
        <groupId>com.dalong</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>bootstrap</artifactId>
 
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <log4j.version>2.17.1</log4j.version>
        <main.class>com.dalong.Application</main.class>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.pf4j</groupId>
            <artifactId>pf4j</artifactId>
            <version>3.6.0</version>
        </dependency>
        <dependency>
            <groupId>com.dalong</groupId>
            <artifactId>service-contract</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>${log4j.version}</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/assembly.xml</descriptor>
                    </descriptors>
                    <appendAssemblyId>false</appendAssemblyId>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attached</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <annotationProcessors>
                        <annotationProcessor>org.pf4j.processor.ExtensionAnnotationProcessor</annotationProcessor>
                    </annotationProcessors>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId> // 配置一些manifest
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>${main.class}</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

assembly.xm

<assembly>
    <id>app</id>
    <formats>
        <format>dir</format>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <useProjectArtifact>false</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <includes>
                <include>*:jar:*</include>
            </includes>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
            <excludes>
                <exclude>*-javadoc.jar</exclude>
                <exclude>*-sources.jar</exclude>
            </excludes>
        </fileSet>
    </fileSets>
</assembly>

入口代码

package com.dalong;
 
import org.pf4j.*;
 
import java.util.List;
import java.util.function.Consumer;
 
public class Application {
    public static void main(String[] args) {
        PluginManager pluginManager = new DefaultPluginManager(){
            @Override
            protected ExtensionFinder createExtensionFinder() {
                DefaultExtensionFinder extensionFinder= (DefaultExtensionFinder) super.createExtensionFinder();
                extensionFinder.addServiceProviderExtensionFinder();// 开启servieloader 模式,
                return extensionFinder;
            }
        };
        pluginManager.loadPlugins();
        pluginManager.startPlugins();
        pluginManager.getPlugins().forEach(new Consumer<PluginWrapper>() {
            @Override
            public void accept(PluginWrapper pluginWrapper) {
                System.out.println("load plugin:"+pluginWrapper.getPluginId()+pluginWrapper.getPluginState());
            }
        });
        List<UserLogin> userLoginList=  pluginManager.getExtensions(UserLogin.class);
        userLoginList.forEach(new Consumer<UserLogin>() {
            @Override
            public void accept(UserLogin userLogin) {
                System.out.println(userLogin.token("name","dalong"));
            }
        });
    }
}

构建&启动

mvn clean package

 

 


运行效果
截取部分

 

 

问题

 
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.5.1</version>
    <configuration>
        <annotationProcessors>
            <annotationProcessor>org.pf4j.processor.ExtensionAnnotationProcessor</annotationProcessor>
        </annotationProcessors>
    </configuration>
</plugin>
 
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>pf4j-learning</artifactId>
        <groupId>com.dalong</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>loginpluginc</artifactId>
 
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <plugin.id>loginpluginc</plugin.id>
        <plugin.class>com.dalong.MyLoginPluginC</plugin.class>
        <plugin.version>0.0.1</plugin.version>
        <plugin.provider>dalong</plugin.provider>
        <plugin.dependencies />
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>com.dalong</groupId>
            <artifactId>service-contract</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>provided</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.pf4j</groupId>
            <artifactId>pf4j</artifactId>
            <version>3.6.0</version>
            <scope>provided</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.hashids</groupId>
            <artifactId>hashids</artifactId>
            <version>1.0.3</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Plugin-Id>${plugin.id}</Plugin-Id>
                            <Plugin-Class>${plugin.class}</Plugin-Class>
                            <Plugin-Version>${plugin.version}</Plugin-Version>
                            <Plugin-Provider>${plugin.provider}</Plugin-Provider>
                            <Plugin-Dependencies>${plugin.dependencies}</Plugin-Dependencies>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

参考资料

https://pf4j.org/doc/getting-started.html
https://github.com/rongfengliang/pf4j-learning
https://docs.oracle.com/javase/tutorial/reflect/member/ctorInstance.html
https://asm.ow2.io/
https://pf4j.org/doc/plugins.html#optional-plugin-dependencies
https://pf4j.org/doc/testing.html

标签:插件,plugin,jar,pf4j,maven,试用,org
来源: https://www.cnblogs.com/rongfengliang/p/15898281.html