Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Karaf Tutorial Part 1 - Is it possible to run tasklist-ui bundle independently from tasklist-persistence bundle? #42

Closed
laurentkvb opened this issue Mar 21, 2018 · 5 comments

Comments

@laurentkvb
Copy link

Hello, I would like to develop an application using the Apache Karaf container combined with a OSGi based application. I would like to know if it is possible to run the "TaskListServlet.java" that is located in the Tasklist-ui module independently from the TaskService, which is from the tasklist-persistence module. For instance, I want to be able to run the TaskListServlet when the persistence is not available. Instead it would show you a custom page or error message.

I don't know how it would look like, but maybe like initializing the 'taskService" only when the tasklist-persistence is available as bundle.

Thank you in advance.

@laurentkvb
Copy link
Author

laurentkvb commented Mar 21, 2018

Currently, I have managed to get it working in the following way:
BundleContext ctx = FrameworkUtil.getBundle(getClass()).getBundleContext();
// @Inject
// @OsgiService
TaskService taskService; -> global variable
....
ServiceReference<?>[] refs = null;
try {
refs = ctx.getServiceReferences(TaskService.class.getName(), null);
} catch (InvalidSyntaxException invalidSyntaxException) {

	}

	if (refs != null) {
		TaskService provider = null;
		for (ServiceReference ref : refs) {
			if (ctx.getService(ref) instanceof TaskService) {
				provider = (TaskService) ctx.getService(ref);
				taskService = provider;
			}
		}
	} else {
		taskService = null;
		writer.println("<h1>Disconnect from the tasklist-persistence plugin </h1>");
	}

Is this the usual way to use a bundle from another plugin? (tasklist-persistence -> using tasklist-ui)

@cschneider
Copy link
Owner

I think you can use an optional service reference in blueprint. This way your servlet also comes up when the service is not present and can then decide what to do.
Try @OsgiService(required=false)

Btw. I just updated the example to the newest version of the blueprint maven plugin and switched to the new annotations.

@laurentkvb
Copy link
Author

Thanks for your response. I have switched over to the newest version of the project and managed to get it working with @reference(availability = Availability.OPTIONAL) annotation. So, it is currently coded that the "task-persistence ui" bundle is an optional service. The call to the "TaskService" method call is now surrounded with a try-catch to handle the ServiceUnavailableException.

Is there a better way to handle the detection of the service that unavailable instead of catching the exception?

@cschneider
Copy link
Owner

cschneider commented Mar 22, 2018 via email

@laurentkvb
Copy link
Author

Thank for the information, it has given me more understanding of Services & Components.
This issue can be closed. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants