javafilenet-p8filenet-content-engine

How can I check if the connection to Filenet was successful?


My Spring Boot application should connect to Filenet, which it is doing fine with the Filenet Java SDK. Now I'd like to implement a health indicator, to periodically check if the connection is still okey. Which leads me to my question:

How do I check if the connection to Filenet is successful?

What I've already tried
Creating a connection and checking if I can get the ObjectStore, which always works and never throws any error:

Connection conn = com.filenet.api.core.Factory.Connection.getConnection("https://...");
Subject subject = UserContext.createSubject(conn, "User", "password", "jaasStanzaName");
UserContext uc = UserContext.get();
uc.pushSubject(subject);
ObjectStore os = Factory.ObjectStore.getInstance(Factory.Domain.getInstance(conn, null), "objectStoreName");

Trying to do any SQL query works sort of, because it throws an EngineRuntimeException.

// Same connection as above
SearchSQL sqlObject = new SearchSQL("SELECT * FROM ...");
SearchScope searchScope = new SearchScope(conn.getObjectStore());
IndependentObjectSet results = searchScope.fetchObjects(sqlObject, null, filter, true);

But using an Exception for logic and method flows seems to me like bad code.

So are there any good methods or tried practices that I am missing?

Thanks in advance


Solution

  • So I eventually solved it myself, although I'm sure there are better ways to do this.

    fnService is a class that essentially does what the code in the question does: creating a connection and executing an SQL query. The query doesn't really have to make much sense, even if it finds nothing it won't throw an exception. It will have to be valid SQL though.

    @Component
    public class FilenetHealthIndicator implements HealthIndicator {
        private final FilenetService fnService;
    
        @Autowired
        public FilenetHealthIndicator(FilenetService fnService) {
            this.fnService = fnService;
        }
    
        @Override
        public Health health() {
            log.debug("Executing automated health check Filenet");
    
            try {
                //We try to make an SQL query, if something doesn't work it will throw an EngineRuntimeException
                this.fnService.searchObjectStoreWithSqlQuery("SELECT Id FROM Document WHERE Id = '{B1111111-1111-1111-AFB1-1111CD1DF11E}'");
            } catch (EngineRuntimeException e){
                log.error("Automated health check cannot connect to Filenet");
                return Health
                        .down(e)
                        .build();
            }
    
            log.debug("Automated health check to Filenet succeeded");
            return Health
                    .up()
                    .build();
        }
    }