Я начал создавать проект OSGI с помощью Spring DM. Я создал два пакета, первый (bundle1) предоставляет службу, которая изменяет порядок получаемой строки. Второй вариант (bundle2) потребляет эту услугу и печатает результат в консоли. Реализация службы:Как запустить Spring service service в OSGI
public final class Bundle1ServiceImpl implements Bundle1Service {
public Bundle1ServiceImpl() {
System.out.println("Bundle1ServiceImpl contructor...");
}
public String scramble(String text) {
List charList = new ArrayList();
char[] textChars = text.toCharArray();
for (int i = 0; i < textChars.length; i++) {
charList.add(new Character(textChars[i]));
}
Collections.shuffle(charList);
char[] mixedChars = new char[text.length()];
for (int i = 0; i < mixedChars.length; i++) {
mixedChars[i] = ((Character) charList.get(i)).charValue();
}
return new String(mixedChars);
}}
весна файл для поставщика услуг:
bundle1-osgi.xml
<service ref="bundle1Service" interface="fr.thispro.bundle1.Bundle1Service" />
bundle1-context.xml
<bean id="bundle1Service" class="fr.thispro.bundle1.internal.Bundle1ServiceImpl">
</bean>
потребитель:
public class Bundle2Consumer {
private final Bundle1Service service;
public Bundle2Consumer(Bundle1Service service) {
this.service = service;
}
public void start() {
System.out.println(service.scramble("Text"));
System.out.println("im started");
}
public void stop() {
System.out.println("im stopped");
}}
файлы пружинные для потребителя: bundle2-context.xml
<beans:bean id="consumer" class="fr.thispro.bundle2.Bundle2Consumer" init-method="start" destroy-method="stop" lazy-init="false" ><beans:constructor-arg ref="bundle1Service"/>
bundle2-osgi.xml
<reference id="bundle1Service" interface="fr.thispro.bundle1.Bundle1Service" />
Служба хорошо зарегестрирована ссылки и обнаружили. Но потребитель ничего не печатает, даже когда я запускаю его явно командой start.
Спасибо, adanvance,
Сложно сказать. Можете ли вы разместить свой пример проекта на github или аналогичном? –
Теперь проект находится в режиме репо: https://github.com/sdahmani/osgi-example-project.git. Я использую команды mvn clean install для его создания и pax-provision для его запуска. Большое спасибо – user1828433