# Creating a Custom Service

To create a service that can be accessed through the `ServiceFactory`, there are a couple of things that need to be configured, below you will see this process using an example service `HelloWorldApi`:

1. Create an interface that extends `ExposedApi`. &#x20;

```java
   package com.my.app

   @JmxManageable
   public interface HelloWorldApi extends ExposedApi {

     @JmxReadable
     public String getHelloWorldString();

     @JmxWritable
     public String getHelloWorldString();

     @JmxInvocable
     public void helloWorld();

   }
```

If you would like your service to be accessible remotely it needs to support being used via jmx. Attivio provides JMX interactivity to classes which use annotations seen above (used appropriately per method depending on desired type of interactivity)

1. Create an implementation of your service that you intend to be used at runtime. &#x20;

```java
   package com.my.app

   import org.slf4j.Logger;
   import org.slf4j.LoggerFactory;

   public class HelloWorldApiImpl {

     private Logger log = LoggerFactory.getLogger(HelloWorldApiImpl.class);
     private String helloWorldString = "hello world";

     public String getHelloWorldString() {
       return helloWorldString;
     }

     public void setHelloWorldString(String helloWorldString) {
       this.helloWorldString = helloWorldString;
     }

     public void helloWorld() {
       log.info(helloWorldString());
     }

   }
```

1. Register the implementation as a bean in the `beans.xml` file (located in the `src/main/resources` folder of the project).

   \`\`\`markup \<?xml version="1.0" encoding="UTF-8"?>

```
 <bean name="sampleService" class="com.bborchard.attiviomodule.SampleAttivioServiceImpl" lazy-init="true"/> <!-- this is the service -->
```

\</beans>

````
4. If desired, create a mock implementation of your service.  


   ```java
   package com.my.app

   import org.slf4j.Logger;
   import org.slf4j.LoggerFactory;

   public class MockHelloWorldApi {

     private Logger log = LoggerFactory.getLogger(MockHelloWorldApi.class);
     private String helloWorldString = "hello from junit test";

     public String getHelloWorldString() {
       return helloWorldString;
     }

     public void setHelloWorldString(String helloWorldString) {
       this.helloWorldString = helloWorldString;
     }

     public void helloWorld() {
       log.info(helloWorldString());
     }

   }
````

Mock implementations are used when testing. They are not strictly necessary if the runtime implementation of a service works in a testing environment.

1. Register your mock in `attivio.test.json`. &#x20;

```javascript
   {
     "testAssociations" : {
       "TestApi" : "com.my.app.MockHelloWorldApi"
     }
   }
```

If no mock is created, the runtime implementation must be specified in this file so that the `ServiceFactory` knows what service implementation to use during testing

1. Access your service using the `ServiceFactory`. &#x20;

```java
   HelloWorldApi helloWorldApi = ServiceFactoryFactory.get().getService(HelloWorldApi.class);
   helloWorldApi.helloWorld();
```

The service should now work whether it is being used in the node process, remotely in a separate process (for example `aie-exec`), or in a junit test.

The implementation in the test association in the `attivio.test.json` file will run during testing.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://attivio.gitbook.io/sdk/release-5.5/service-factory/custom-service-creating.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
