Created A Service Which Adds 2 numbers.How Do I Call My Service?

So I have used the basicexample module,as my project structure and created a Service called AddServiceImpl.I have a method add(int i,int j).It returns the addition of i and j.So how do I call the method by passing the values of i and j?
@dipakthapa @shruthipitta

Where are you calling the service from?? You can inject the service in the class where you will be calling the method from.

In org.openmrs.module.basicmodule.service I have AddService.java
package org.openmrs.module.basicmodule.service;

import org.openmrs.api.OpenmrsService;

public interface AddService{

int add(int num1,int num2);

}

In org.openmrs.module.basicmodule.service.impl I Have AddServiceImpl

package org.openmrs.module.basicmodule.service.impl;

import org.openmrs.module.basicmodule.service.AddService;

public class AddServiceImpl implements AddService {

public int add(int i,int j) {
	
	return i+j;
}

}

So how do I call my add() method?I have no idea on how to call the method.Thanks for your reply!!

Add your implementation as a service. You can do that by using the @Service annotation which is present in Spring package. After doing that, your “AddService” service will be exposed by the Spring container. Now, all you need to do is

@Service

public class SomeServiceImpl implements SomeService {

@Autowired

private AddService addService;

public int doSomething() {

return addService.add(1,2);

}

}

In above example I am calling the add method from my new class SomeServiceImpl.

So, you can simply just autowire the services and call the methods present in those services.

If you are planning to call this service from the Controller which is present in omod package then first you have to add this module as dependency in pom.xml of omod module. Then you can simply Autowire the services from api module.

Thanks!Will update you after I run it

After uploading the module,it says unable to find module activator.What did I do wrong?

@krishnanspace You need to add a module activator in your api module. This activator extends BaseModuleActivator. Some thing like this

package com.demo;

public class DemoModuleActivator extends BaseModuleActivator {

private Log log = LogFactory.getLog(this.getClass());

    public void startup() {
  log.info("Starting Module");

}

public void shutdown() { log.info(“Shutting down Module”); }

}

Now you need to add this file in config.xml in your omod module.

<activator> com.demo.DemoModuleActivator </activator>

You can see the details here.

The basic module I had cloned had the Activator but it was implementing Activator which is deprecated.So thats why I got the error.Then I changed it to extends BaseModuleActivator and the module was uploaded.But I got this error after uploading the module:

https://pastebin.com/2J8AtS4y

I think your container has not registered AddService bean. Can you verify whether your applicationContext.xml has component scan on your package or class? Something like this

<context:component-scan base-package=“your.package” />

Also verify whether your service is annotated with @Service annotation.