HackToday Walk Blog


  • Home

  • Tags

  • Archives

  • Search

OSGI的tracking service使用

Posted on 2012-12-16

在eclipse的OSGI框架中,tracking service是OSGI很重要的一个功能,通过对服务的查询来动态的获取相应服务,例子:

我们建立两个bundle,一个是提供sayHello的服务,另外一个bundle来使用sayHello服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bundle 1: com.javaworld.sample.service

结构如下:

├── META-INF
│   └── MANIFEST.MF
└── src
    └── com
        └── javaworld
            └── sample
                └── service
                    ├── HelloService.java
                    └── impl
                        ├── HelloServiceActivator.java
                        ├── HelloServiceFactory.java
                        └── HelloServiceImpl.java
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
bundle 2: com.javaworld.sample.helloworld

├── build.properties
├── META-INF
│   └── MANIFEST.MF
└── src
    └── com
        └── javaworld
            └── sample
                └── helloworld
                    ├── Activator.java
                    └── HelloServiceTracker.java

HelloServiceTracker 继承 osgi的ServiceTracker, 通过构造函数中说明需要track哪些服务,

  public HelloServiceTracker(BundleContext context) {
        super(context, HelloService.class.getName(), null);
   }
 
  public Object addingService(ServiceReference reference) {
        System.out.println("Inside HelloServiceTracker.addingService " +
                                        reference.getBundle());
        return super.addingService(reference);
    }
  
  public void removedService(ServiceReference reference, Object service) {
        System.out.println("Inside HelloServiceTracker.removedService " +
                                          reference.getBundle());
        super.removedService(reference, service);
    }

在helloservice中,HelloServiceActivator的bundle启动过程中会注册服务,
helloServiceRegistration = context.registerService(HelloService.class.getName(),
helloServiceFactory, null);

helloworld的bundle就可以通过service tracker的 getService来返回相应的service对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    /*
     * (non-Javadoc)
     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
     */
    public void start(BundleContext context) throws Exception {
        System.out.println("Hello World!!");
        helloServiceTracker= new HelloServiceTracker(context);
        helloServiceTracker.open();
        HelloService helloService = (HelloService)helloServiceTracker.getService();
        System.out.println(helloService.sayHello());
    }
   
    /*
     * (non-Javadoc)
     * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
     */
    public void stop(BundleContext context) throws Exception {
        System.out.println("Goodbye World!!");
        helloServiceTracker.close();
    }

上次写了忘记给参考资料,实在抱歉。

参考资料:

  1. http://www.javaworld.com/javaworld/jw-03-2008/jw-03-osgi1.html?page=5

Maven的POM介绍

Posted on 2012-12-15

POM: Project Object Model 是maven项目架构里重要的基本单元

最小的POM需具备以下的元素:

  • project root
  • modelVersion - should be set to 4.0.0
  • groupId - the id of the project’s group.
  • artifactId - the id of the artifact (project)
  • version - the version of the artifact under the specified group

例如:

1
2
3
4
4.0.0 
com.mycompany.app
my-app
1

POM 使用 :: 来唯一标记一个项目,它们是maven的
因为软件经常涉及到很多projects,那么如何来维护projects之间的关系非常重要,POM中有两个非常重要的特性:
project inheritance和project aggregation,两者主要是看待projects之间的关系的角度不同,其实是殊途同归。

假设前提,我们有两个projects,com.mycompany.app:my-module:1 com.mycompany.app:my-app:1
其中 com.mycompany.app:my-app:1 是com.mycompany.app:my-module:1 的 parent artifact

(1) 组织形式如下的时候

1
2
3
. |-- my-module 
| `-- pom.xml
`-- pom.xml

首先看看project inheritance: my-module的pom需要加入parent的元素,com.mycompany.app:my-module:1’s POM 如下

1
2
3
4
5
6
7
8
 com.mycompany.app 
my-app
1

4.0.0
com.mycompany.app
my-module
1

对于project aggregation:通过在my-app中指定相应的子module,从而使得 parent project 知道所有的 modules 里面
很重要的一点是在packaging里指出为pom

1
2
3
4
5
6
7
4.0.0
com.mycompany.app
my-app
1
pom

my-module

(2) 组织形式如下的时候:

1
2
3
4
. |-- my-module 
| `-- pom.xml
`-- parent
`-- pom.xml

首先看看project inheritance:
通过relativepath来配合设置相应的目录结构

1
2
3
4
5
6
7
8
com.mycompany.app 
my-app
1
.../parent/pom.xml


4.0.0
my-module

对于project aggregation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
   4.0.0 
com.mycompany.app
my-app
1
pom

../my-module
```

(3) 你也可以将inheritance和aggregation联合起来,应用下面三条:



Specify in every child POM who their parent POM is.
Change the parent POMs packaging to the value "pom" .
Specify in the parent POM the directories of its modules (children POMs)

对于如下的形式:

. |– my-module
| -- pom.xml– parent
`– pom.xml

1
2

com.mycompany.app:my-app:1's POM

4.0.0
com.mycompany.app
my-app
1
pom

../my-module

1
2

com.mycompany.app:my-module:1's POM

 com.mycompany.app 
 my-app 
 1 
 ../parent/pom.xml 

4.0.0 
my-module 

`

参考资料:

  1. http://maven.apache.org/guides/introduction/introduction-to-the-pom.html
  2. http://www.oracle.com/technetwork/cn/community/java/apache-maven-getting-started-1-406235-zhs.html

openstack nova的unit测试 ----确保配置合适debug的sqlite

Posted on 2012-12-02
  1. 问题

在你进行unittest的时候,尤其涉及到database相关的调试,总发现nova的没有生成database,其实主要是nova默认的会使用sqlite的内存内型数据库

参考sqlite的资料发现
e = create_engine(‘sqlite://‘)

The sqlite :memory: identifier is the default if no filepath is present. Specify sqlite:// and nothing else:

  1. 解决

为了调试数据库更加简单,你可以配置不使用内存型的数据库

在 nova 的TestCase (nova/nova/test.py)

fake_flags.set_defaults(FLAGS) —>

配置类似如下的sql连接,nova.sqlite是对应的sqlite数据库文件

1
conf.set_default('sql_connection', "sqlite:////var/lib/nova/nova.sqlite")

配置前面的前提是你自己的环境已经有这个数据库文件,如果没有的话,你需要手动创建一个
步骤如下:

(1) change your sql connection in nova.conf to be sqlite
(2) nova-manage db sync
这样你对应的nova.conf设置的位置生成相应的数据库文件
(3) cp nova.sqlite clean.sqlite

完毕,这样进行unit测试最后的结果就会写到sqlite数据库文件里的。

  1. 附加资料

如何查看sqlite数据库?

很多工具可以查看, 我用了两个都还可以,一个是sqliteman,前面的一篇文章已经写过了。
另外一个是SQLite Manager,这个就是一个firefox的插件,
https://addons.mozilla.org/en-us/firefox/addon/sqlite-manager/
很容易就可以安装为firefox的插件。

安装sqliteman

Posted on 2012-11-28

sqliteman 是一款小巧的图形化管理sqlite 数据库的软件,安装相对简单
因为自己使用redhat6.3,从官方给的那个源死活yum不行,就使用源代码编译安装了。 具体如下:
sqliteman的编译安装依赖:

1
2
cmake
qt

还有stdlibc的东东

注明: 如果没有 Qscintilla2 libraries and header files, 需要使用编译选项来从源代码安装

安装过程如下:

安装依赖的软件

1)yum install cmake.x86_64
2)yum install compat-libstdc++-33.x86_64
3)yum install qt-devel.x86_64

编译

4)cmake -DWANT_INTERNAL_QSCINTILLA=1

5)make

6) make install

然后就可以使用了

redhat6.3 安装mysql-workbench

Posted on 2012-11-09

redhat6.3缺少 libzip.so.1, 而这个是 mysql-workbench所需要的, 所以你需要首先安装 libzip包.

方法:

  1. 下载rpm包

http://rpm.pbone.net/index.php3/stat/4/idpl/17359193/dir/fedora_16/com/libzip-0.9.3-3.fc15.x86_64.rpm.html

  1. yum install libzip-0.9.3-3.fc15.x86_64.rpm
  2. yum install mysql-workbench-gpl-5.2.44-1el6.x86_64.rpm

参考资料:

http://databaseblog.myname.nl/2011/03/mysql-workbench-on-rhel6.html

ubuntu 12.04 install python 2.6

Posted on 2012-10-20

http://www.ubuntututorials.com/install-python-2-6-ubuntu-12-04/

  1. Run below command to add PPA to your repository
1
sudo add-apt-repository ppa:fkrull/deadsnakes
  1. Then you can update your repository and install Python 2.6
1
2
sudo apt-get update
sudo apt-get install python2.6 python2.6-dev

rabbitmq epmd error for host

Posted on 2012-10-16

今天迁移虚拟机,发现死活rabbitmq起不来,提示,

1
ERROR: epmd error for host "****": timeout (timed out establishing tcp connection)

后来google发现,

https://gist.github.com/2522701

主机名和ip不匹配了,需要更改/etc/hosts

1
127.0.0.1 yournewhostname

eclipse中openstack一些模块的unresolved

Posted on 2012-09-23

openstack的git下载的源码导入到eclipse中有时会一些模块unresolved
比如paste,发现使用pip下载后的paste的package中没有init.py ,很奇怪

于是就从网上下载对应版本paste源码包,然后将init.py 导入, 执行
python setup.py install
就会对其编译,生成init.pyc
重启eclispe,可以源码跳转到paste了,但是还是有个 Unresolved import: deploy

具体还没想出怎么回事,不过暂时不影响使用。

openstack winpdb调试

Posted on 2012-09-23

http://lists.openstack.org/pipermail/openstack-dev/2012-August/000794.html
看到了关于winpdb的相关调试:

Launch Winpdb GUI # winpdb Start Debugging

1
2
# cd /home/openstack-dev/workspace/nova/bin 
# winpdb -d -r

Set Password: openstack

Attach to Process

1) In Winpdb GUI, select File -> Attach
2) Enter password from step 4, should display files associated with that password
3) Select process/file then select OK

To Stop Debug or Make Changes and Restart

1) File -> Stop to detach current debugger
2) Make changes in Eclipse and Save (make sure development user has write privileges to repository)
3) kill and restart winpdb debugging process started above 4) Re-attach to the process in the Winpdb GUI

也许自己不太会用,感觉比较难用。而且有时就崩溃了。还是eclipse方便些。

Linux误删系统文件修复

Posted on 2012-08-25

因不注意删除了 /var/lib 下的文件,所以需要找个方法解决,

虽然,在删除资料后,不推荐在对磁盘的写操作,但是机器的硬盘的因为特殊原因不能自行拆卸,也没有实现安装修复软件。所以只能先搞安装修复软件了。

首先,原来的/var/lib下有yum相关的文件,导致无法yum安装,
解决如下:

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
http://www.ogre.com/taxonomy/term/33

yum failures with missing $releasever
Posted June 15th, 2011 by leif

    Fedora
    Linux

During an upgrade (yum update) on a Fedora VM, something went horribly
wrong, and it crashed in the middle of the update. After rebooting, and
cleaning up the mess, yum still was very unhappy. Running an update
would give me errors like

Could not parse metalink https://mirrors.fedoraproject.org/metalink?repo=fedora-$releasever&arch=x86_64 error was
No repomd file
Error: Cannot retrieve repository metadata (repomd.xml) for repository: fedora. Please verify its path and try again

Very odd. It turns out, $releasever was not properly set, and I could
not figure out why. Poking around, I realized that $reelasever is
supposed to come from examining the version number of a particular RPM
package, in my case fedora-release. Well, lo and behold, this package
was no longer installed on my box, yum must have uninstalled it, but
crashed before installing the new update (or something...). I mounted
the Fedora Core DVD, and simple reinstalled the missing package, and
things are happy joy joy again. Here's the command:

$ sudo rpm -i ./Packages/fedora-release-13-1.noarch.rpm

其次,找到 http://hi.baidu.com/cnjxxf/item/7b92070f59a09390a3df4392
extundelete 修复ext4 ext3
那么就可以安装这个软件,然后修复了。 修复后的会放在 RECOVERED_FILES一个目录里,
然后我copy到/var/lib,毕竟对磁盘做了一些写操作(安装上述的软件及依赖),所以有些文件估计恢复不了。

然后,我检查了恢复差不多,然后在重启机器前,把所有的数据备份好,以防止机器起不来。

好在机器能起来,但是无法进入桌面系统。 好像gdm出问题了,重装gdm也不行,最后想到kdm。
安装kdm后,重启后就可以进入桌面了。

1…161718…26

Kai Qiang Wu

This is a place for thinking and writing

253 posts
32 tags
GitHub
© 2020 Kai Qiang Wu
Powered by Hexo
|
Theme — NexT.Gemini v5.1.4
Visitor Total Visit