Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json validation provider not found #40

Open
anitadc opened this issue Dec 2, 2019 · 12 comments
Open

Json validation provider not found #40

anitadc opened this issue Dec 2, 2019 · 12 comments
Assignees
Labels
question Further information is requested

Comments

@anitadc
Copy link

anitadc commented Dec 2, 2019

I am facing same problem when running Spring boot application jar from command prompt. But It is working fine from eclipse and same application jar in other machine. I am using java11 and no module structure.

Originally posted by @anitadc in #18 (comment)

@leadpony
Copy link
Owner

leadpony commented Dec 2, 2019

@anitadc
Thank you for contacting me.
Are you using Spring Boot Maven Plugin in your build?

@leadpony leadpony self-assigned this Dec 2, 2019
@anitadc
Copy link
Author

anitadc commented Dec 3, 2019

Yes. I am using spring-boot-maven-plugin 2.1.2

@leadpony
Copy link
Owner

leadpony commented Dec 3, 2019

@anitadc
Thank you for your reply.
I ported the code sample Basic Parser to a Spring Boot application.
Please find the attatched project.
justify-examples-spring-boot.zip
It seems to work fine. Please tell me if there exists difference from your project.

@anitadc
Copy link
Author

anitadc commented Dec 5, 2019

Difference is - I am calling this justify parser asynchronously using completablefuture and external executor

@leadpony
Copy link
Owner

leadpony commented Dec 5, 2019

Can you provide me with a sample project that reproduces the reported problem?

@anitadc
Copy link
Author

anitadc commented Dec 6, 2019

To generate issue, initialize JsonValidationService within child thread instead of static or Singleton object.

Do below changes In your sample project -
https://github.com/leadpony/justify/files/3916858/justify-examples-spring-boot.zip

CompletableFuture.runAsync(()-> {
JsonValidationService service = JsonValidationService.newInstance() ;
.......
validate(arg1[0], arg2[1]);
.....
})
.exceptionally((Throwable e)->
LOG.error(e);
return null;
});

@leadpony
Copy link
Owner

leadpony commented Dec 8, 2019

Thank you @anitadc
With your help, I reproduced the problem finally using both spring-boot-maven-plugin and CompletableFuture.
However the same problem occurs even without Justify.
Could you please run the following code?

@SpringBootApplication
public class Application implements CommandLineRunner {

    private static final Logger LOG = LoggerFactory.getLogger(Application.class);

    @Override
    public void run(String... args) throws Exception {
        var future = CompletableFuture.runAsync(() -> {
            JsonProvider provider = loadProvider();
            LOG.info(provider.getClass().getName());
        }).exceptionally(e -> {
            LOG.error(e.getMessage());
            return null;
        });
        future.get();
    }

    private JsonProvider loadProvider() {
        ServiceLoader<JsonProvider> loader = ServiceLoader.load(JsonProvider.class);
        Iterator<JsonProvider> it = loader.iterator();
        if (it.hasNext()) {
            return it.next();
        } else {
            throw new IllegalStateException("No service provider found");
        }
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

pom.xml (with Justify removed)

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>jakarta.json</groupId>
            <artifactId>jakarta.json-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>jakarta.json</artifactId>
            <classifier>module</classifier>
            <version>${jakarta.json.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

Therefore this is NOT a problem of Justify. I suspect that the problem is due to a restriction or bug of spring-boot-maven-plugin using with ServiceLoader class.

@leadpony leadpony added the question Further information is requested label Dec 8, 2019
leadpony added a commit that referenced this issue Dec 8, 2019
@leadpony
Copy link
Owner

leadpony commented Dec 9, 2019

I added a fallback logic to JsonValidationService#newInstance().
Could you please test with the current snapshot? See Building from Source section in the top page of this repository.

@anitadc
Copy link
Author

anitadc commented Dec 10, 2019

Thank you @anitadc
With your help, I reproduced the problem finally using both spring-boot-maven-plugin and CompletableFuture.
However the same problem occurs even without Justify.
Could you please run the following code?

@SpringBootApplication
public class Application implements CommandLineRunner {

    private static final Logger LOG = LoggerFactory.getLogger(Application.class);

    @Override
    public void run(String... args) throws Exception {
        var future = CompletableFuture.runAsync(() -> {
            JsonProvider provider = loadProvider();
            LOG.info(provider.getClass().getName());
        }).exceptionally(e -> {
            LOG.error(e.getMessage());
            return null;
        });
        future.get();
    }

    private JsonProvider loadProvider() {
        ServiceLoader<JsonProvider> loader = ServiceLoader.load(JsonProvider.class);
        Iterator<JsonProvider> it = loader.iterator();
        if (it.hasNext()) {
            return it.next();
        } else {
            throw new IllegalStateException("No service provider found");
        }
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

pom.xml (with Justify removed)

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>jakarta.json</groupId>
            <artifactId>jakarta.json-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>jakarta.json</artifactId>
            <classifier>module</classifier>
            <version>${jakarta.json.version}</version>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

Therefore this is NOT a problem of Justify. I suspect that the problem is due to a restriction or bug of spring-boot-maven-plugin using with ServiceLoader class.

Correct. This will happen with any code like justify which will use ServiceLoader and current thread classloader.

@anitadc
Copy link
Author

anitadc commented Dec 10, 2019

I added a fallback logic to JsonValidationService#newInstance().
Could you please test with the current snapshot? See Building from Source section in the top page of this repository.

I appreciate this change. Hopefully this will work. I will confirm you.

@anitadc
Copy link
Author

anitadc commented Dec 10, 2019

Did you add this fallback logic in 2.1.0 version?? Kindly publish this in maven repository.

@leadpony
Copy link
Owner

leadpony commented Dec 10, 2019

@anitadc
You need to build and install it to your local repository by yourself.
If it works well with your application, I will publish it as the next official release.
Please follow the instruction in the section Building from Source.
You also need to change the version of the dependency in your pom.xml to 2.1.0-SNAPSHOT
Thank you for your cooperation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants