Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to share dao and service layer

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have spring boot web app and now going to develop another app which uses the same database and is going to perform some scheduled tasks. May be I'm be using spring boot (@scheduled), may be not. The problem is not to duplicate classes for work with the database. Is it possible to share dao and services layers and what's the best practice to implement this?
 
Ekaterina Galkina
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget to tell: spring web app exposes rest services. May be to use them somehow?
 
Ekaterina Galkina
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the second app may be rest consumer.
But also I've heard about HttpInvoker...May be it's better?
 
Saloon Keeper
Posts: 12165
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not make a module out of your service layer and declare a dependency on it?
 
Ekaterina Galkina
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Why not make a module out of your service layer and declare a dependency on it?


I doubted because they are beans which are instantiated inside ApplicationContext. Is it possible to separate them and instantiate also in the ApplicationContext of the second app? It seems yes...

 
Ekaterina Galkina
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The whole task is the following:

A database and a webapp  are on the first server.
The second application is on he second server and may be started on the 3th server, 4th server, etc (as meny as needed). It monitors sites on schedule and produces images, this job requers a lot of CPU, RAM and disk space. The schedule is stored in the database.
If one of the servers is down the scheduled job must be redirected to a server which is alive.
The result of the job must be recorded to the database, including the case when something went wrong.
If something goes wrong admin should be notified.

I'm not sure if it's enough information to make a desicion about architecture, but I'm reading about JMS, chron,  spring quartz. There are a log of questions for me here. For example, why to use activemq (if to use it): if server with activemq broker is down then messages are lost anyway, right? Then why?
Or can application be restarted automatically after server reboot without chron?
 
Ranch Hand
Posts: 128
Hibernate Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Stephan has suggested, you can have a service layer module and your application can use that as a dependency. You can use clustering on the application server for routing requests when the other servers are down.
 
Ekaterina Galkina
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joseph Mokenela wrote:As Stephan has suggested, you can have a service layer module and your application can use that as a dependency. You can use clustering on the application server for routing requests when the other servers are down.


Do you mean something like this https://docs.oracle.com/cd/A97329_03/core.902/a92171/cluster.htm (About Oracle9iAS Clustering)?
 
Saloon Keeper
Posts: 22289
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simplest way to share database services is to make them into discrete projects that produce JARs. These JAR files can then be included into the process of manufacturing the various apps that use them, in the same way that Spring's own various JARs are. Maven, for example, handles this sort of arrangement very well.

There are some obvious limitations here. One is that it's assuming that all of the services are probably local to the app that's using them and that there are no shared interactions between the services of different apps. You also cannot bundle in a sharable cache for the database services. And, of course, updates to the service libraries generally mean having to update all the apps and possibly restart some servers.

One of the earliest ways to share resources and services that have interactions constraints between multiple apps in Enterprise Java was Enterprise JavaBeans. EJBs got a lot of bad press over the years, but that's in large part because they were used as the proverbial hammer in the hands of a small child. EJBs are very well suited for providing remotable resources and services and now, of course, also local ones (the JPA standard is a subset of EJB 3.0). Spring generally discourages EJBs over other alternatives, some of which are widely considered as more capable, but the choice is yours. EJBs do require an EJB-compliant container, though.

There are other remoting possibilities that are also popular. RMI is effectively EJB without the "bean" part, JMS and MQ technologies are good where asynchronous and high-latency communications are allowable. And of course web services, which are language-independent (as was the late, unlamented, CORBA).

One of the best ways to find options is simply to look at the Spring website for the major functional families (such as Spring Persistence), then see what sub-families look good to you.


Bundling up resources into separate JARs doesn't hide them from Spring. Spring looks for stuff using the standard classpath services, so if you add Spring annotations to the classes, Spring will treat them just like direct inclusions (of course it does need to know about scannable package paths just like in direct source inclusion).

I'd have to check, but I believe that you can also put fractional application context files in a JAR's META-INF directory. And/or declare/override application context in the application's own application context files (or whatever context-defining mechanism your BeanFactory is using).
 
Well THAT's new! Comfort me, reliable tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic