Access Service in Groovy Console | Community
Skip to main content
Level 2
November 24, 2022
Solved

Access Service in Groovy Console

  • November 24, 2022
  • 1 reply
  • 797 views

Hi All,

Is it really possible to access/invoke OSGI service from groovy console? I have been in situation where every time I needed to change/configure page along with some configuration - this becomes very tedious process to me just to access my service result. So I want to know if any possibilities which resolve my problem.

Thanks in advance! 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SantoshSai

Hi @shoib_i ,

Indeed! It is possible, I remember in my blog I used to demonstrate through groovy console to have it shown quickly.

Here I have one example.
Let's say we have a component in AEM as below

package com.training.osgi.core.stringgen;

import org.osgi.service.component.annotations.Component;

@Component(immediate = true, service = StringGenerator.class)
public class StringGeneratorImpl implements StringGenerator {

    @Override
    public String generateString() {
        return "Version 1.1";
    }
}

You can then invoke it through groovy console as below

import com.training.osgi.core.stringgen.StringGenerator;
def service = gerService(StringGenerator.class);
println service.generateString();

You must see below output

Version 1.1

For more details please watch my published video here: https://www.techinnovia.com/osgi-services-and-components/

Hope that helps!

Regards,

Santosh

1 reply

SantoshSai
Community Advisor
SantoshSaiCommunity AdvisorAccepted solution
Community Advisor
November 24, 2022

Hi @shoib_i ,

Indeed! It is possible, I remember in my blog I used to demonstrate through groovy console to have it shown quickly.

Here I have one example.
Let's say we have a component in AEM as below

package com.training.osgi.core.stringgen;

import org.osgi.service.component.annotations.Component;

@Component(immediate = true, service = StringGenerator.class)
public class StringGeneratorImpl implements StringGenerator {

    @Override
    public String generateString() {
        return "Version 1.1";
    }
}

You can then invoke it through groovy console as below

import com.training.osgi.core.stringgen.StringGenerator;
def service = gerService(StringGenerator.class);
println service.generateString();

You must see below output

Version 1.1

For more details please watch my published video here: https://www.techinnovia.com/osgi-services-and-components/

Hope that helps!

Regards,

Santosh

Santosh Sai