一个"应用&QUOT ;,多个软件包,数据库同步 [英] One "application", multiple packages, database synchronisation

查看:147
本文介绍了一个"应用&QUOT ;,多个软件包,数据库同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要:按顺序与服务器端的多个SQLite数据库的同步

Summary: synchronisation of multiple SQLite databases with server side sequentially.

我工作的那注定是模块化的Andr​​oid应用程序:我们有一个启动程序关闭触发意向,根据用户的选择,启动不同的应用模块(单独的安装包)。因为它是现在,每个模块都有自己的SQLite数据库和工作正常。我分配到的任务,需要制作使用MS同步框架4.0(目前月CTP)掌上电脑和服务器端(SQL Server 2008 R2中)之间的数据同步。我曾根据同步框架规范开发的Andr​​oid lib和的作品了。

I'm working on an Android application that is bound to be modular: we have a launcher that fires off intents, based on user choice, to start different application modules (separate installable packages). As it is now, each module has its own SQLite database and that works fine. The task I'm assigned to requires making data synchronisation between handheld and server side (SQL Server 2008 R2) using MS Sync Framework 4.0 (currently October CTP). I had developed android lib according to Sync Framework specs and that works too.

我的问题是,因为这种松耦合的设计(和其他一些限制)我需要在发射器的按钮时,强制所有模块进行数据库同步一个接一个(顺序无所谓现在)。

The issue I have is that because of this loosely coupled design (and some other restrictions) I need to have a button in launcher, that forces all modules to synchronise their databases one-by-one (order doesn't matter for now).

我目前的做法是有一个抽象的广播接收器和放大器;服务(我现在才发现IntentService)是继承中的每个模块课程。因此,在发射器I播出的意图,每个模块捡起来使用自定义广播接收器,并使用,同样,个性化的服务同步其数据库......在平行的。我已经检查排序后的广播,但我有一个服务做实际工作的它并没有真正的帮助。我目前能想到的唯一的另一种方法是有一个全系统的互斥体,并用它在每一个服务来锁定同步调用。

My current approach is to have an abstract BroadcastReceiver & Service (I only now discovered IntentService) classes that are inherited in each modules. So in launcher I broadcast intent, each module picks it up using customized BroadcastReceiver and syncs its database using, again, customized Service... In parallel. I have checked ordered broadcasts, but as I have a service doing the actual work it doesn't really help. The only other way I can currently think of is to have a system-wide mutex and use it to lock sync call in every service.

这是我的第一款Android相关的任务,以便有可能是一个更好的办法来解决这个问题,我也不会从重新设计同步一部分运行如果让我们的团队未来的生活更容易一点。

This is my first Android related task so there probably is a better way to solve this, I wouldn't run from redesigning synchronisation part if that makes our teams future life a bit easier.

[修改]因此,它看起来像Java不支持命名的互斥。

[EDIT] So it looks like Java doesn't support named mutexes.

[ EDIT2 ]通过模块(或单独的安装包),我的意思是每个模块不同的APK。所以,在开始一个模块当你真正为它开始一个新的进程。

[EDIT2] By modules (or separate installable packages) I meant different APK for each module. So when starting a module you actually start a new process for it.

推荐答案

在最后我不得不使用服务器套接字这样来实现全局锁:

At the end I had to implement global lock using server socket like this:

ServerSocket mServerSocket;

/**
 * Simulates global locking using server socket
 * @return If lock was successful
 */
private synchronized boolean lock() {
  try {
    Log.v(serviceName, "Trying to acquire a lock...");
    // any port from 49152 through 65535 should work
    mServerSocket = new ServerSocket(51515);
    return true;
  } catch (IOException ioe) {
    return false;
  }         
}

/**
 * Simulates global unlocking
 */
private synchronized void unlock() {
  try {
    Log.v(serviceName, "Releasing a lock.");
    if (mServerSocket != null) {
      if (!mServerSocket.isClosed()) {
        mServerSocket.close();
      }
      mServerSocket = null;     
    }
  } catch (IOException e) {
    e.printStackTrace();
  }
}

这篇关于一个"应用&QUOT ;,多个软件包,数据库同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆