11 Replies Latest reply: Jan 21, 2010 1:36 PM by Nikolay Elenkov RSS

    Seam + Quartz + Postgresql

    Jonne Deprez Newbie

      I got Quartz 1.6.0 to work Seam 2.1.1 with a Postgresql 8.3 database to store the jobs. This is how I got it to work:


      Add quartz.jar to your project. In my project the folder is called EarContent.


      Using JobStoreCMT is necessary when working with Seam, because you don't use Seam to handle database access for quartz. Quartz database access is handled by the container (the JBoss server).
      You need to create 2 separate databases: one for use in Seam and one for quartz. Make sure the database for quartz is in the public shema.


      Add <async:quartz-dispatcher/> to components.xml.


      Define quartz.jar as a module in application.xml. Just add
      <module>
        <ejb>quartz.jar</ejb>
      </module>

      to the list of modules.


      The two datasources are defined in myapplication-ds.xml.
      Mine looks like:


      <datasources>
      <local-tx-datasource>
            <jndi-name>myappDatasource</jndi-name>
            <connection-url>jdbc:postgresql://localhost:5432/myapp</connection-url>
            <driver-class>org.postgresql.Driver</driver-class>
            <user-name>seamuser</user-name>
            <password>password</password>
         </local-tx-datasource>
         
         <xa-datasource>
            <jndi-name>quartzDatasource</jndi-name>
            <track-connection-by-tx/>
            <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
            <xa-datasource-property name="ServerName">localhost</xa-datasource-property>   
            <xa-datasource-property name="PortNumber">5432</xa-datasource-property>
            <xa-datasource-property name="DatabaseName">quartz</xa-datasource-property>     
            <xa-datasource-property name="User">postgres</xa-datasource-property>
            <xa-datasource-property name="Password">password</xa-datasource-property>     
            <metadata>
             <type-mapping>PostgreSQL 8.0</type-mapping> 
            </metadata>
         </xa-datasource>
      </datasources>



      In the ejbModule folder I created a seam.quartz.properties file with the following contents:


      #============================================================================
      # Configure Main Scheduler Properties
      #============================================================================
      
      org.quartz.scheduler.instanceName MyAppQuartzScheduler
      org.quartz.scheduler.instanceId AUTO
      org.quartz.scheduler.rmi.export false
      org.quartz.scheduler.rmi.proxy false
      
      #============================================================================
      # Configure ThreadPool
      #============================================================================
      
      org.quartz.threadPool.class org.quartz.simpl.SimpleThreadPool
      org.quartz.threadPool.threadCount 5
      org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread true
      
      #============================================================================
      # Configure JobStore
      #============================================================================
      
      org.quartz.jobStore.misfireThreshold 60000
      
      org.quartz.jobStore.class org.quartz.impl.jdbcjobstore.JobStoreCMT
      org.quartz.jobStore.driverDelegateClass org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
      org.quartz.jobStore.useProperties false
      org.quartz.jobStore.dataSource quartzDatasource
      org.quartz.jobStore.tablePrefix qrtz_
      org.quartz.jobStore.nonManagedTXDataSource quartzDatasource
      
      
      #============================================================================
      # Configure Datasources
      #============================================================================
      
      org.quartz.dataSource.quartzDatasource.jndiURL java:/quartzDatasource



      Last small detail: to persist handles to jobs in postgresql, annotate the getter of the entity bean with @Lob and use the oid type type in postgresql.


      That's it! 



        • 1. Re: Seam + Quartz + Postgresql
          Nikolay Elenkov Master

          Jonne Deprez wrote on Jun 29, 2009 13:01:


          I got Quartz 1.6.0 to work Seam 2.1.1 with a Postgresql 8.3 database to store the jobs.



          Cool :)


          You should use the newest version (1.6.5), it has quite a few bugfixes (including performance and safety related).



          The two datasources are defined in myapplication-ds.xml.
          Mine looks like:

          <datasources>
          <local-tx-datasource>
                <jndi-name>myappDatasource</jndi-name>
             ....
             
             <xa-datasource>
                <jndi-name>quartzDatasource</jndi-name>
              ....
          </datasources>



          In the ejbModule folder I created a seam.quartz.properties file with the following contents:

          ...
          org.quartz.jobStore.nonManagedTXDataSource quartzDatasource
          




          Your nonManagedTXDataSource is actually an <xa-datasource>, which is wrong. The reason it is called 'nonManagedTX' is that Quartz
          manages the transactions, not your container. If you specify an <xa-datasource> it will appear to work fine,
          but you will get all sorts of nasty-looking transaction errors when you least expect it (because Quratz will not be able to rollback the transaction, for example). You should define a separete, non-managed datasource for Quartz. Something like this:


          <no-tx-datasource> 
           <jndi-name>noTxquartzDatasource</jndi-name>
          </no-tx-datasource>
          



          And then seam.quartz.properties should then look like this:


          ...
          org.quartz.jobStore.dataSource quartzDatasource
          org.quartz.jobStore.tablePrefix qrtz_
          org.quartz.jobStore.nonManagedTXDataSource noTxQuartzDatasource
          ...
          
          



          (Yes you do need two different datasources)


          And you do not need to create two separate databases, just two datasources (having separate databases is OK, too) .


          HTH

          • 2. Re: Seam + Quartz + Postgresql
            Jonne Deprez Newbie

            Thanks for the information Nikolay! I'll make the corrections.

            • 3. Re: Seam + Quartz + Postgresql
              Jonne Deprez Newbie

              Concerning the seam.quartz.properties file, you still speak about quartzDatasource.
              Summarizing: for the entities in my web application I need a tx-datasource myappDatasource, and for running quartz I need a xa-datasource quartzDatasource as well as a no-tx-datasource noTxQuartzDatasource?


              What do you mean with



              having separate databases is OK, too

              Do you mean it is possible to use two databases for quartz? Do they contain the same tables?


              Thx!

              • 4. Re: Seam + Quartz + Postgresql
                Nikolay Elenkov Master

                Jonne Deprez wrote on Jul 07, 2009 11:00:


                Concerning the seam.quartz.properties file, you still speak about quartzDatasource.
                Summarizing: for the entities in my web application I need a tx-datasource myappDatasource, and for running quartz I need a xa-datasource quartzDatasource as well as a no-tx-datasource noTxQuartzDatasource?



                myappDatasource and quartzDatasource can be the same. The quartzDatasource does not have to be a xa-datasource, a local-tx-datasource
                will do if you are not using clustering. To summarize: you need at least two datasources, but you can have more (say, three).




                What do you mean with

                having separate databases is OK, too

                Do you mean it is possible to use two databases for quartz? Do they contain the same tables?



                'separate' as in one for Quartz and one for your application. I can' use two databases for Quartz.
                The other alternative is to have only one database: it hosts your application tables and the Quartz tables.


                HTH

                • 5. Re: Seam + Quartz + Postgresql
                  gdfgd eqwe Novice
                  HI!

                  I try to set Seam +Quartz, but I aways get the following exception:

                    <no-tx-datasource>
                        <jndi-name>noTxquartzDatasource</jndi-name>
                     </no-tx-datasource>

                  org.jboss.seam.Component.newInstance(Component.java:2132)
                       ... 169 more
                  Caused by: org.quartz.SchedulerConfigException: Failure occured during job recovery. [See nested exception: org.quartz.JobPersistenceException: Failed to obtain DB connection from data source 'noTxquartzDatasource': java.sql.SQLException: There is no DataSource named 'noTxDvcQuartzDatasource' [See nested exception: java.sql.SQLException: There is no DataSource named 'noTxDvcQuartzDatasource']]



                  • 6. Re: Seam + Quartz + Postgresql
                    gdfgd eqwe Novice
                    the correct error is


                    Caused by: org.quartz.SchedulerConfigException: Failure occured during job recovery. See nested exception:

                    org.quartz.JobPersistenceException: Failed to obtain DB connection from data source 'noTxquartzDatasource': java.sql.SQLException:

                    There is no DataSource named 'noTxquartzDatasource' [See nested exception: java.sql.SQLException: There is no DataSource named 'noTxquartzDatasource']
                    • 7. Re: Seam + Quartz + Postgresql
                      gdfgd eqwe Novice

                      my problem was solved in quartz forum

                      • 8. Re: Seam + Quartz + Postgresql
                        Andrew Martin Newbie

                        I am also having the same problem but does not seem to find the solution. Could you please provide a link to the solution?

                        • 9. Re: Seam + Quartz + Postgresql
                          Shervin Asgari Master

                          I don't get it.


                          I am running asynchronous calls in my Seam app (running JBoss) fine without the need to create separate database, and even adding the module in application.xml


                          My calls are created just fine. But since I am not doing what you have proposed here, does that mean that I can never ensure that the calls will get run? Or is it to do with persistence called asynchronously?

                          • 10. Re: Seam + Quartz + Postgresql
                            Shervin Asgari Master

                            Nikolay Elenkov wrote on Jul 07, 2009 12:28:



                            Jonne Deprez wrote on Jul 07, 2009 11:00:


                            Concerning the seam.quartz.properties file, you still speak about quartzDatasource.
                            Summarizing: for the entities in my web application I need a tx-datasource myappDatasource, and for running quartz I need a xa-datasource quartzDatasource as well as a no-tx-datasource noTxQuartzDatasource?



                            myappDatasource and quartzDatasource can be the same. The quartzDatasource does not have to be a xa-datasource, a local-tx-datasource
                            will do if you are not using clustering. To summarize: you need at least two datasources, but you can have more (say, three).


                            Two questions.
                            1. How should this quartz db or table look like?
                            2. I cannot locate any ejbModule folder in my seam-gen app. Could you specifiy further where I can put the quartz property file.

                            • 11. Re: Seam + Quartz + Postgresql
                              Nikolay Elenkov Master

                              Do you need job persistence? If not, use the (default) RAMJobStore. If you do: download the Quartz distribution from their website. It has the DDL scripts, as well as examples. Then put the properties file in you ejb-jar.


                              HTH