How to make Prime numbers with Flow Service?

Hi everyone,

I’m trying to make a Prime number using Flow service to check whether a given input number is Prime or not, is it possible? if so, how do I make it?


I’d suggest that you create a Java service instead, but you can look for any generic Java snippet online and adapt it to Flow language.


1 Like

Hi, thanks for the reply.

I’m still confused about how to implement the java snippet code that I got online and adapt it to flow language

Create a java service via Designer → New Java Service.

I’m using the java code that I copied from here Java Program to Check Whether a Number is Prime or Not

public static void main(String[] args) {

    int num = 29;
    boolean flag = false;
    for (int i = 2; i <= num / 2; ++i) {
      // condition for nonprime number
      if (num % i == 0) {
        flag = true;

    if (!flag)
      System.out.println(num + " is a prime number.");
      System.out.println(num + " is not a prime number.");

I then paste it into shared area of my java service, renamed and made num into an argument, as well as returning flag as boolean (rather than printing to out) i.e.

Now I paste the following code into the java service body, so that I can map the service inputs and call the isPrime function and map it’s output to a isPrime pipeline variable.

        // pipeline in
		IDataCursor pipelineCursor = pipeline.getCursor();
		String numStr = IDataUtil.getString(pipelineCursor, "num");

		// body
		boolean isPrime = false;
		try {
			isPrime = isPrime(Integer.parseInt(numStr));
		} catch(Exception e) {
			throw new ServiceException(e);
		// pipeline out
		IDataUtil.put(pipelineCursor, "isPrime", isPrime);

Don’t forget to update the services signature as so

Now you invoke it just like any other service.


Hi Mr. John thanks for the answer, I really appreciate it.

I know I can use the java service, but my original plan was to fully make it on Flow Service so I can understand flow service better.

Anyway, is it possible that the result is shown in server logs like this? “11 is a prime number”, if so… how?

There is no java API to access the server log, however you can invoke the debugLog service from within your java code.

Here is an example method that could be used as part of log4j as a ServerLogAppender class.

public void logToServerLog(LoggingEvent event) {
		// Bug in wm, means that if we try to invoke services outside of service-pool we get a null pointer
		// exception. Following if fixes the problem by ensuring we have session and uses assigned.

		if (event.getLevel().toInt() == Level.ERROR_INT || event.getLevel().toInt() == Level.FATAL_INT)
			if (event.getThrowableInformation() != null && event.getThrowableInformation().getThrowable() != null)
		IData input = IDataFactory.create();
		IDataCursor inputCursor = input.getCursor();
		IDataUtil.put(inputCursor, "message", event.getMessage());
		IDataUtil.put(inputCursor, "function", event.getLoggerName());
		IDataUtil.put(inputCursor, "level", "0");


		try {
			ServiceUtils.invokeService(input, "pub.flow", "debugLog", null, null, true);	// don't use session causes null pointer exception in certain cases
		} catch( Exception e) {
			ServerAPI.logError(new ServiceException ("Couldn't log debug info to server log : " + e));