订阅所有JSP/Servlet的日志 订阅 | 这是最新一篇日志 上一篇 | 下一篇日志 下一篇 ]
插件技术

OSGi开发之Event:事件处理[原]

首先配置OSGi Framework的运行环境,我的Eclipse是3.3的,运行环境需要以下的Bundle:

? org.eclipse.osgi
? org.eclipse.equinox.ds
? org.eclipse.equinox.registry
? org.eclipse.equinox.common
? org.eclipse.equinox.log
? org.eclipse.osgi.services
? org.eclipse.equinox.event
? org.eclipse.osgi.util


操作步骤:点击菜单Run->Open Run Dialog…,在左侧的列表中,找到OSGi Framework,右键单击,New,修改Name为EventDemo,并在Bundles选项卡中,选中以上列举的Bundle,然后点击Apply,点Run。

在Console中输入ss来查看我们的环境是否成功运行:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
osgi> ss
 
Framework is launched.
 
id	State       Bundle
0	ACTIVE      org.eclipse.osgi_3.3.1.R33x_v20070828
1	ACTIVE      org.eclipse.equinox.common_3.3.0.v20070426
2	ACTIVE      org.eclipse.equinox.log_1.0.100.v20070226
3	ACTIVE      org.eclipse.equinox.ds_1.0.0.qualifier
4	ACTIVE      org.eclipse.equinox.registry_3.3.1.R33x_v20070802
5	ACTIVE      org.eclipse.osgi.services_3.1.200.v20070605
6	ACTIVE      org.eclipse.equinox.event_1.0.100.v20070516
7	ACTIVE      org.eclipse.osgi.util_3.1.200.v20070605
 
osgi>


OK,这样我们的Spring-OSGi的环境就启动好了,现在先关闭它。我们来建立一个Plug-in Project,命名为:EventPublisher。

首先新建一个类:com.demo.eventpublisher.Log,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.demo.eventpublisher;
 
import org.osgi.framework.BundleContext;
import org.osgi.service.log.LogService;
import org.osgi.util.tracker.ServiceTracker;
 
/**
 * Utility class with static methods for logging to LogService, if available 
 * 
 * @version $Revision: 1.1 $
 */
public class Log {
 
	static private ServiceTracker logTracker;
 
	private Log() {
	}
 
	public static void init(BundleContext bc) {
		logTracker = new ServiceTracker(bc, LogService.class.getName(), null);
		logTracker.open();
	}
 
	static void dispose() {
		if (logTracker != null) {
			logTracker.close();
		}
		logTracker = null;
	}
 
	public static void log(int level, String message) {
		log(level, message, null);、
	}
 
	public static void log(int level, String message, Throwable e) {
		LogService logService = (LogService) logTracker.getService();
		if (logService != null) {
			logService.log(level, message, e);
		}
	}
 
	public static void info(String message)
	{
		info(message, null);
	}
 
	public static void info(String message, Throwable e)
	{
		log(LogService.LOG_INFO, message, e);
	}
}

接下来创建一个循环发布事件的线程:com.demo.eventpublisher.TimerEvent,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.demo.eventpublisher;
 
import java.util.Calendar;
import java.util.Hashtable;
 
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;
import org.osgi.util.tracker.ServiceTracker;
 
public class TimerEvent extends Thread implements BundleActivator {
    Hashtable time = new Hashtable();
    ServiceTracker tracker;
 
    public TimerEvent() {
        super("TimerEvent");
    }
 
    public void start(BundleContext context) {
        Log.init(context);
        tracker = new ServiceTracker(context, EventAdmin.class.getName(), null);
        tracker.open();
        start();
    }
 
    public void stop(BundleContext context) {
        interrupt();
        tracker.close();
        Log.dispose();
    }
 
    public void run() {
        while (!Thread.interrupted())
            try {
                Log.info("Rasie Event");
                Calendar c = Calendar.getInstance();
                set(c, Calendar.MINUTE, "minutes");
                set(c, Calendar.HOUR, "hours");
                set(c, Calendar.DAY_OF_MONTH, "day");
                set(c, Calendar.MONTH, "month");
                set(c, Calendar.YEAR, "year");
                EventAdmin ea = (EventAdmin) tracker.getService();
                if (ea != null)
                    ea.sendEvent(new Event("com/demo/timer", time));
                Thread.sleep(60000 - c.get(Calendar.SECOND) * 1000);
            }
            catch (InterruptedException e) {
                // 忽略
            }
    }
 
    void set(Calendar c, int field, String key) {
        time.put(key, new Integer(c.get(field)));
    }
}


在Plug-in ManiFest Editor中打开META-INF/MANIFEST.MF,如果安装了PDE,将会默认使用Plug-in ManiFest Editor打开。将Activator设置为com.demo.eventpublisher.TimerEvent,此时MANIFEST.MF文件中的内容如下:
1
2
3
4
5
6
7
8
9
10
11
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: EventPublisher Plug-in
Bundle-SymbolicName: EventPublisher
Bundle-Version: 1.0.0
Bundle-Activator: com.demo.eventpublisher.TimerEvent
Import-Package: org.osgi.framework;version="1.3.0",
 org.osgi.service.event;version="1.1.0",
 org.osgi.service.log;version="1.3.0",
 org.osgi.util.tracker;version="1.3.2"
 

注意:手工写MANIFEST.MF文件时,最后一行一定要有一个回车。

接下来我们再来建立一个Plug-in Project,命名为EventHander。

我们建立一个类,名为:com.demo.eventhandler.CustomEventHandler,代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.demo.eventhandler;
 
import java.util.Dictionary;
import java.util.Hashtable;
 
import org.osgi.framework.BundleContext;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;
 
import com.demo.internal.Log;
 
public class CustomEventHandler implements EventHandler
{
    public final static String [] topics = new String[] { 
//        "org/osgi/service/log/LogEntry/LOG_INFO", 
//        "org/osgi/service/log/LogEntry/LOG_WARNING",
//        "org/osgi/framework/ServiceEvent/REGISTERED",
//        "org/osgi/framework/ServiceEvent/MODIFIED",
//        "org/osgi/framework/ServiceEvent/UNREGISTERING",
//        "org/osgi/framework/FrameworkEvent/STARTED",
//        "org/osgi/service/log/LogEntry/LOG_ERROR",
        "com/demo/timer"}; 
    
    public void registry(BundleContext context)
    {
        Dictionary d = new Hashtable(); 
        d.put(EventConstants.EVENT_TOPIC, topics ); 
 
        context.registerService( EventHandler.class.getName(), this, d ); 
    }
    public void handleEvent(Event event)
    {
        if ("com/acme/timer".equals(event.getTopic()))
        {
            Log.info("Received event, EventTopic " + event.getTopic() + " " + event.getProperty("year") + "-" + event.getProperty("month") + "-" + event.getProperty("day"));
        }
        else
        {
            Log.info("Received event, EventTopic " + event.getTopic());
        }  
    }
}


我们再建立一个Activator,名为:com.demo.eventhandler.internal.Activator,代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.demo.internal;
 
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
 
import com.demo.eventhandler.CustomEventHandler;
 
 
public class Activator implements BundleActivator
{
    public void start(BundleContext context) throws Exception
    {
        Log.init(context);
        CustomEventHandler eventhandler = new CustomEventHandler();
        eventhandler.registry(context);
    }
 
    public void stop(BundleContext context) throws Exception
    {
        Log.dispose();
    }
}

OK,我们将新建立的这两个Bundle加入到运行环境中,Run。
在Console中等待大约60秒后,输入log后可以在最后找到如下日志信息:
引用原文:

>Info [1] Rasie Event initial@reference:file:../../../../../../../../Documents/workspace/EventPublisher/
>Info [7] Received event, EventTopic com/demo/timer initial@reference:file:../../../../../../../../Documents/workspace/ConsumerServiceDemo/


瞧,我们已经收到打印出来的事件接收信息了 Received event, EventTopic com/demo/timer

平均得分
(0 次评分)





文章来自: 本站原创
标签: event OSGi EventHandler 
评论: 12 | 查看次数: 576
  • 共有 12 条评论
游客 [2008-11-10 15:37:08]
游客 [2008-10-30 13:57:39]
Air Jordan Shoes
Warhammer Gold
Warhammer Online Gold
Warhammer Accounts
Warhammer Power Leveling
Warhammer Online Key
Warhammer Gold
Warhammer Online Gold
Warhammer Time Card
Warhammer CD Key
WAR gold
warhammer online gold
Buy WAR gold
Buy warhammer gold
Buy warhammer online gold
warhammer gold
WAR Accounts
warhammer Accounts
warhammer online Accounts
Buy WAR Accounts
warhammer Accounts for sale
Warhammer Power Leveling
Warhammer Online Power Leveling
War Power Leveling
Buy Warhammer Power Leveling
Warhammer PowerLeveling
Cheap Warhammer Power Leveling
Cheap Warhammer Online Power Leveling
Buy War Power Leveling
Warhammer EU Power Leveling
Cheap Warhammer Gold
Cheap Warhammer online gold
Buy Cheap Warhammer Gold
Buy WAR Gold
Warhammer EU Gold
Warhammer EU Power Leveling
Warhammer EU CD Key
Warhammer EU Accounts
Warhammer CD Key
Warhammer online CD Key
Warhammer Timecard
Buy Warhammer Time Card
Warhammer 60 days Time Card
Cheap WAR Accounts
Cheap warhammer Accounts
Cheap warhammer online Accounts
Buy Cheap WAR Accounts
buy Warhammer CD Key
buy Warhammer online CD Key
cheap Warhammer CD key
warhammer time card
Warhammer prepaid time card
Buy Lotro Gold | Lord Of The Rings Online Gold
Lotro Accounts
| Buy Lotro Accounts
Lord Of The Rings Online Power Leveling | Lord Of The Rings Online PowerLeveling
Lotro Cd Key | Lord Time Card
Lotro Gold | Lotro Gold Instant Delivery
lord of the rings online accounts | lord of the rings online accounts for sale
Lotro Power Leveling | Lotro Powerleveling
Lord Of The Rings Online Cd Key | Lord Of The Rings Online Time Card
Lord of the Rings Online Gold
Buy Lotro Gold
Sell LoTRO Gold
LoTRO CD Key
LoTRO Europe Gold
Cheap LoTRO Accounts
Lord of the Rings Online Power Leveling
Lord of the Rings online CD Key
Cheap Lotro Gold
wow power leveling
wow power leveling
游客 [2008-10-30 13:57:03]
wow power leveling
wow power leveling
Buy Lotro Gold | Lord Of The Rings Online Gold
Lotro Accounts
| Buy Lotro Accounts
Lord Of The Rings Online Power Leveling | Lord Of The Rings Online PowerLeveling
Lotro Cd Key | Lord Time Card
Lotro Gold | Lotro Gold Instant Delivery
lord of the rings online accounts | lord of the rings online accounts for sale
Lotro Power Leveling | Lotro Powerleveling
Lord Of The Rings Online Cd Key | Lord Of The Rings Online Time Card
Lord of the Rings Online Gold
Buy Lotro Gold
Sell LoTRO Gold
LoTRO CD Key
LoTRO Europe Gold
Cheap LoTRO Accounts
Lord of the Rings Online Power Leveling
Lord of the Rings online CD Key
Cheap Lotro Gold
Air Jordan Shoes
Warhammer Gold
Warhammer Online Gold
Warhammer Accounts
Warhammer Power Leveling
Warhammer Online Key
Warhammer Gold
Warhammer Online Gold
Warhammer Time Card
Warhammer CD Key
WAR gold
warhammer online gold
Buy WAR gold
Buy warhammer gold
Buy warhammer online gold
warhammer gold
WAR Accounts
warhammer Accounts
warhammer online Accounts
Buy WAR Accounts
warhammer Accounts for sale
Warhammer Power Leveling
Warhammer Online Power Leveling
War Power Leveling
Buy Warhammer Power Leveling
Warhammer PowerLeveling
Cheap Warhammer Power Leveling
Cheap Warhammer Online Power Leveling
Buy War Power Leveling
Warhammer EU Power Leveling
Cheap Warhammer Gold
Cheap Warhammer online gold
Buy Cheap Warhammer Gold
Buy WAR Gold
Warhammer EU Gold
Warhammer EU Power Leveling
Warhammer EU CD Key
Warhammer EU Accounts
Warhammer CD Key
Warhammer online CD Key
Warhammer Timecard
Buy Warhammer Time Card
Warhammer 60 days Time Card
Cheap WAR Accounts
Cheap warhammer Accounts
Cheap warhammer online Accounts
Buy Cheap WAR Accounts
buy Warhammer CD Key
buy Warhammer online CD Key
cheap Warhammer CD key
warhammer time card
Warhammer prepaid time card
游客 [2008-10-29 15:16:41]
Expertise Rating. buy eve isk As explained back. ever quest platinum when it was implemented. guild wars gold Expertise reduces your opponent’s chances . guild wars gold to parry and dodge. HDRO gold Think of it as another. level wow Hit Rating. lineage 2 adena. While a mob cannot . lord of rings online gold parry attacks . lotro gold made from behind. lotro gold they can dodge them. lotro gold Since every point of Expertise. lotro po lowers their chance. power level to dodge by 0. powerleveling 25%. powerleveling wow you’ll need 26 Expertise . runescape gold or 104 Expertise Rating . runescape money to eliminate their. wow europe chance to dodge. wow level That’s 63 more Expertise. wow level Rating from gear . wow lvl if you pick up Weapon Expertise. wow lvl You can pick up 44 Expertise . wow power leveling Rating from just the [Shard of Contempt]. wow powerlevel from Magister’s Terrace. wow powerlevel Critical Strike Rating. Critical Strike Rating.
游客 [2008-10-29 15:16:24]
Strength. cheap wow power leveling If you see items . des po wow with Strength on them. free online games PASS. free online war games These are meant. gold wow for DPS Warriors or Ferals. gold wow Strength only gives you . gold wow 1 Attack Power . mp3 player for each point. online games You’re better . play war games off with other gear. po wow Trust me. power level Stamina. wow geld You should . wow geld get enough of . wow gold verkaufen this elsewhere. wow level service Do not try . wow leveling get to any more. wow leveling service Do not consider . wow lvl this stat . wow lvl 60 when you look at gear. wow lvl 70 It shouldn’t matter. wow power leveling Armor. wow powerlevel See Stamina. wow powerleveling If Blizzard made . wow schnell gold DPS cloth. wowgold I’d suggest you.
游客 [2008-10-29 15:16:06]
Ogres down south. 8GB MP3 PLAYER Ogres down south. apple ipod in Badlands. buy cheap wow gold next to the earth . canon digital camera elementals. cheap world of warcraft gold they drop a bit of . digital camera mageweave and silver. digital cameras but as all other ogres . dvd player they die really fast. eve isk so they are nice . ipod to grind. ipod nano A lot of casters. ipod shuffle amoung them which. ipod touch is always nice. ipods Mithril spawn . mp3 in the cave often . mp3 player and a chest every 15 minutes. mp3 players Feralas. mp4 ogres down south at the big ruin. portable dvd players 45-47 is their lvls. world of warcraft buy gold drop decent mageweave. wow let me tell . wow gold what you're getting. wow gold A Mage at low levels. wow leveling is a giant. wow powerleveling unmitigated wuss. zubehoer mp3 player I'm not even kidding.
游客 [2008-10-29 15:15:41]
Feralas. 2GB MP3 PLAYER ogres down south . 4GB MP3 PLAYER at the big ruin. buy wow gold 45-47 is their lvls. buying gold world of warcraft drop decent mageweave. cell phones and if u go down. cheap cell phones to the cave all . cheap wow gold the way down . cheap wow gold at the bottom. cheap wow gold u will almost only see . cheapest wow gold casters inside. eve isk Because of the packs u may get. mp3 players I suggest . phones cell not going here . portable mp3 player til the stated lvls. portable mp3 players instead of when. sell wow gold ur the same lvl . world of warcraft gold as the mobs. wow which would usually . wow gold be my suggested lvl. wow gold range to grind them. wow gold Also the warlords. wow gold call for help . wow gold when they get low. wow gold attracting other ogres. wow gold kaufen that are near. wow gold kaufen is all it's cracked up to be.
游客 [2008-10-11 09:07:01]
游客 [2008-09-28 13:59:40]
游客 [2008-09-22 15:06:43]
coolflyr_reg [2008-08-22 11:25:57]
没错,不过既然要作OSGi,那么PDE是前提!

不过我是从更新Eclipse的更新里面更新下来得PDE,如果缺少包的话,完全可以去Eclipse的CVS上Checkout一个出来,或者使用maven来使其自动下载依赖的包,但是依赖于apache。
游客 [2008-08-14 16:20:29]
eclipse要装rcp版本的,要不然缺少osgi包
  • 共有 12 条评论
发表评论
昵 称:  登录
内 容:
选 项:
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启