package org.o7planning.tutorial.helloosgi.mathconsumer;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;public class Activator implements BundleActivator private static BundleContext context; static BundleContext getContext() return context; public void start(BundleContext bundleContext) throws Exception Activator.context = bundleContext; System.out.println("MathConsumer Starting..."); System.out.println("MathConsumer Started"); public void stop(BundleContext bundleContext) throws Exception Activator.context = null; System.out.println("MathConsumer Stopped");
MathConsumer là một trong những OSGi nó cũng khá được Call là một Bundle. Bây giờ họ đang knhì báo MathConsumer thực hiện một trong những Bundle khác mục đích nhằm rất có thể chạy MathConsumer sau này.

Bạn đang xem: Osgi là gì


org.eclipse.osgiorg.eclipse.equinox.consoleorg.apađậy.felix.gogo.commandorg.apabịt.felix.gogo.runtimeorg.apabít.felix.gogo.shell
*

*

*

*

*

Như hình minh họa chúng ta thấy ID của OSGi MathConsumer là 2, thực hiện lệnh "stop" để dừng lại Bundle này.
package org.o7planning.tutorial.helloosgi.mathservice;public interface MathService public int sum(int a, int b);
package org.o7planning.tutorial.helloosgi.mathservice.impl;import org.o7planning.tutorial.helloosgi.mathservice.MathService;public class MathServiceImpl implements MathService {
package org.o7planning.tutorial.helloosgi.utils;public class MathUtils public static int minus(int a, int b) return a- b;
Đăng ký dịch vụ MathService nhằm những OSGi khác hoàn toàn có thể sử dụng. Việc này được tiến hành vào Activator của OSGi MathService.

Xem thêm: Cara Mematikan Background Intelligent Transfer Service Là Gì


package org.o7planning.tutorial.helloosgi;import org.o7planning.tutorial.helloosgi.mathservice.MathService;import org.o7planning.tutorial.helloosgi.mathservice.impl.MathServiceImpl;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;public class Activator implements BundleActivator private static BundleContext context; static BundleContext getContext() return context; public void start(BundleContext bundleContext) throws Exception Activator.context = bundleContext; System.out.println("Registry Service MathService..."); this.registryMathService(); System.out.println("OSGi MathService Started"); private void registryMathService() MathService service = new MathServiceImpl(); context.registerService(MathService.class, service, null); public void stop(BundleContext bundleContext) throws Exception Activator.context = null; System.out.println("OSGi MathService Stopped!");
Cấu hình để xuất khẩu (export) 2 packages org.o7planning.tutorial.helloosgi.utils với org.o7planning.tutorial.helloosgi.mathservice.OSGi y như một chiếc hộp đóng góp, các OSGi khác chỉ rất có thể áp dụng các class/interface của OSGi này giả dụ nó ở trong những package được xuất khẩu ra bên ngoài.
OSGi MathConsumer chỉ có thể áp dụng các class/interface ở trong số package cơ mà MathService xuất khẩu.