在jboss模块中使用计时器

时间:2017-02-10 15:48:50

标签: timer jboss java-ee-6

我需要在jboss(JEE6)中创建一个计时器/计划服务。问题是,它被部署为jboss模块。我知道如果部署为EJB

,以下代码可以工作
public class MainActivity extends AppCompatActivity {

    private ProgressBar mProgressBar;
    private BottomListViewAdapter mBottomAdapter;
    private View mDividerView;
    EmployeeDBHandler dbHandler;
    private int mStartingEmployeeID = mStartingID;
    private String table = "employees";
    private static final int LOADER_INTEGER = 1;
    private Cursor mBottomCursor, mTopCursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
        mDividerView = findViewById(R.id.divider);
        dbHandler = new EmployeeDBHandler(getApplicationContext());
        mProgressBar.setVisibility(View.VISIBLE);
        mDividerView.setVisibility(View.GONE);
        getXMLData();

        //GUI for seeing android SQLite Database in Chrome Dev Tools
        Stetho.InitializerBuilder inBuilder = Stetho.newInitializerBuilder(this);
        inBuilder.enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this));
        Stetho.Initializer in = inBuilder.build();
        Stetho.initialize(in);
    }

    public void getXMLData() {
        OkHttpClient client = getUnsafeOkHttpClient();
        Request request = new Request.Builder()
                .url(getString(R.string.API_FULL_URL))
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                final String responseData = response.body().string();
                final InputStream stream = new ByteArrayInputStream(responseData.getBytes());
                final XMLPullParserHandler parserHandler = new XMLPullParserHandler();
                final ArrayList<Employee> employees = (ArrayList<Employee>) parserHandler.parse(stream);

                for (Employee e : employees) {
                    dbHandler.addEmployee(e);

                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mProgressBar.setVisibility(View.GONE);
                        mDividerView.setVisibility(View.VISIBLE);
                        displayTopList();
                        displayBottomList();
                    }
                });
            }
        });
    }

    public void displayTopList() {
        SQLiteDatabase db = dbHandler.getWritableDatabase();
        mTopCursor = db.rawQuery("SELECT * FROM " + table + " WHERE " + "Employee_number" + "=" + mStartingEmployeeID, null);
        ListView mTopListView = (ListView) findViewById(R.id.mTopList);
        TopListCursorAdapter topAdapter = new TopListCursorAdapter(this, mTopCursor);
        mTopListView.setAdapter(topAdapter);
    }

    public void displayBottomList() {
        SQLiteDatabase db = dbHandler.getWritableDatabase();
        mBottomCursor = db.rawQuery("SELECT * FROM " + table + " WHERE " +
                "Employee_number" + "!=" + mStartingEmployeeID + " AND " +
                "Manager_employee_number" + "=" + mStartingEmployeeID + " ORDER BY " +
                "Last_name" + " ASC", null);

        ListView mBottomListView = (ListView) findViewById(R.id.mDirectReportList);
        mBottomAdapter = new BottomListViewAdapter(this, mBottomCursor);
        mBottomListView.setAdapter(mBottomAdapter);
        mBottomAdapter.notifyDataSetChanged();

    }
}

但如果另外部署则不起作用。是否有任何建议或标准方法在jboss模块中创建和使用计时器?我想避免使用core-java方法创建TimerTask。

1 个答案:

答案 0 :(得分:0)

创建jboss模块,以便以有效的方式跨应用程序共享代码。有关更多信息,请参阅here。现在,因为模块没有部署在EJB容器中;这里不可能使用EJB调度程序。在我们的例子中,可能的解决方案或方法是创建一个依赖于这些jboss模块的EJB,其方法将由调度程序调用。

例如,

@Singleton
@Startup
public class NotificationRecorderBean {


 @Inject
 private MyJbossModule myJbossModule;

 @Schedule(hour="*/1", persistent=false)
    public void execute() {
        LOGGER.debug("Recording notification count");
        myJbossModule.process();
   }

}