venerdì 31 agosto 2012

applicazione war con due entity manager contemporanemente

ambiente: jboss-7.1.1 
applicazione: war con due entity, due session bean repository, un service rest che opera usando entrambi i session bean.
caricare il driver mysql tra i moduli di jboss7

(scarica da qui: https://github.com/fiorenzino/dual-jpa.git)

ricordarsi di:
1) persistence.xml aggiungere i nomi delle classi che deve gestire ciascun em
2) persistence.xml non usare <property name="hibernate.hbm2ddl.auto" value="update" /> altrimenti crea le tabelle in entrambi i db (consiglio di crearle prima di deployare)


esempio di entity:

@Entity
public class UserA implements Serializable {

private Long id;
private String name;
....


@Entity
public class UserB implements Serializable {

private Long id;
private String name;

....



esempio di persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="PuA">
<jta-data-source>java:jboss/datasources/ExampleA</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
<class>it.coopservice.test.model.UserA</class>
</persistence-unit>
<persistence-unit name="PuB">
<jta-data-source>java:jboss/datasources/ExampleB</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
<class>it.coopservice.test.model.UserB</class>
</persistence-unit>
</persistence>

3) nei repository iniettare gli entity manager specificando la persistence unit:

esempio di session bean: 



@Stateless
@LocalBean
public class SessionA implements Serializable {
@PersistenceContext(unitName = "PuA")
protected EntityManager em;
...



@Stateless
@LocalBean
public class SessionB implements Serializable {
@PersistenceContext(unitName = "PuB")
protected EntityManager em;

...


4) nello standalone.xml mappare i datasource usando driver XA (altrimenti non sarà possibile effettuare operazioni nella stessa transazione su entrambi i datasource:

esempio di datasource in standalone.xml:


<datasources>
<xa-datasource jndi-name="java:jboss/datasources/ExampleA" pool-name="ExampleAD" enabled="true" use-ccm="false">
<xa-datasource-property name="URL">jdbc:mysql://localhost:3306/exampleA</xa-datasource-property>
<driver>com.mysql</driver>
<xa-pool>
   <is-same-rm-override>false</is-same-rm-override>
   <interleaving>false</interleaving>
   <pad-xid>false</pad-xid>
   <wrap-xa-resource>true</wrap-xa-resource>
</xa-pool>
<security>
   <user-name>root</user-name>
   <password>flower</password>
</security>
<recovery>
   <recover-credential>
<user-name>root</user-name>
<password>flower</password>
   </recover-credential>
</recovery>
    </xa-datasource>
<xa-datasource jndi-name="java:jboss/datasources/ExampleB" pool-name="ExampleBD" enabled="true" use-ccm="false">
<xa-datasource-property name="URL">jdbc:mysql://localhost:3306/exampleB</xa-datasource-property>
<driver>com.mysql</driver>
<xa-pool>
   <is-same-rm-override>false</is-same-rm-override>
   <interleaving>false</interleaving>
   <pad-xid>false</pad-xid>
   <wrap-xa-resource>true</wrap-xa-resource>
</xa-pool>
<security>
   <user-name>root</user-name>
   <password>flower</password>
</security>
<recovery>
   <recover-credential>
<user-name>root</user-name>
<password>flower</password>
   </recover-credential>
</recovery>
    </xa-datasource>
</datasources>



 <drivers>
                    <driver name="com.mysql" module="com.mysql">
                        <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
                    </driver>
 </drivers>




a questo punto sara' possibile usare un service che si inettta entrambi i session bean delegati a ciascun respository e fare operazioni transazionali:


esempio di service che opera su entrambi i session beans:

@Stateless
@LocalBean
public class ServiceAB implements Serializable {

@Inject
SessionA sessionA;

@Inject
SessionB sessionB;

public String createAB(String nameA, String nameB) {
UserA userA = new UserA(nameA);
Long idA = sessionA.persist(userA);
UserB userB = new UserB(nameB);
Long idB = sessionB.persist(userB);
return "a:" + idA + " - b:" + idB;
}
}



esempio di servizio rest per fare i test :


@Path("/v1/test")
@Stateless
@LocalBean
public class Rest implements Serializable {

@Inject
SessionA sessionA;
@Inject
SessionB sessionB;

@Inject
ServiceAB serviceAB;

@GET
@Path("/addA/{name}")
@Produces(MediaType.TEXT_PLAIN)
public Long addA(@PathParam("name") String name) {
UserA userA = new UserA();
userA.setName(name);
return sessionA.persist(userA);
}

@GET
@Path("/addB/{name}")
@Produces(MediaType.TEXT_PLAIN)
public Long addB(@PathParam("name") String name) {
UserB userB = new UserB();
userB.setName(name);
return sessionB.persist(userB);
}

@GET
@Path("/addAB/{nameA}/{nameB}")
@Produces(MediaType.TEXT_PLAIN)
public String addAB(@PathParam("nameA") String nameA,
@PathParam("nameB") String nameB) {
return serviceAB.createAB(nameA, nameB);
}
}

ricordarsi di aggiungere un rest activator:


@ApplicationPath("/rest")
public class JaxRsActivator extends javax.ws.rs.core.Application {

}






giovedì 23 agosto 2012

appunti git

1) nel caso in cui esiste gia' un reporisoty locale e ci vuoi attacare un repo remoto
Create a new repository on the command line
touch README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/fiorenzino/b-flower.git
git push -u origin master

Push an existing repository from the command line
git remote add origin https://github.com/fiorenzino/b-flower.git
git push -u origin master


per distrugger repository remoti
git push <remote> :<remote_branch>

es. git push origin :FORGE-610

per aggiungere al precedente commit:
git commit --amend

mercoledì 22 agosto 2012

turbo maven


mvn -T 4 clean install
mvn -T 2C clean install

and skip test:

-Dmaven.test.skip=true

Skipping tests in some modules in Maven


What about skipping tests only in this module ?
In the pom.xml of this module:
<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.4.2</version>
        <configuration>
          <skipTests>true</skipTests>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]</project>

http://stackoverflow.com/questions/772735/skipping-tests-in-some-modules-in-maven


ubuntu update sun jdk


chmod 777 jdk-6u33-linux-x64.bin
./jdk-6u33-linux-x64.bin
ls
mv jdk1.6.0_33 /usr/lib/jvm/
sudo mv jdk1.6.0_33 /usr/lib/jvm/
cd /usr/lib/jvm/
ls
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.6.0_33/bin/javac 1
sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.6.0_33/bin/java 1
sudo update-alternatives --install /usr/bin/javaws javaws /usr/lib/jvm/jdk1.6.0_33/bin/javaws 1
sudo update-alternatives --config javac
sudo update-alternatives --config java
sudo update-alternatives --config javac
sudo update-alternatives --config javaws

mercoledì 11 luglio 2012

creare modulo attivo (con annotazioni) in jboss 7.1.1.final

jandex versione 1.0.3.final (non la 1.0.4 - elimina il jar senza dire nulla).

- produce il file idx e lo inserisce nel jar sotto meta-inf
java -jar jandex-1.0.3.Final.jar -m -v commons2Web-1.6.2.jar

aggiungerlo come modulo.

per gestire la dipendeza nell'applicazione che la usa, creare il file jboss-deplyment-structure.xml (sotto web-inf) e aggiungere la voce:


<module name="it.coopservice.commons2Web" export="true" slot="no-renderer" annotations="true">
       <imports>
               <include path="*" />
       </imports>
</module>



per riferimenti:
https://community.jboss.org/thread/196129

giovedì 28 giugno 2012

openshift: hot deploy

Da provare!!
basta aggiungere un solo file (hot_deploy) nella cartella .openshift/markers/ per evitare che openshift faccia il reboot dell'application server.


bisogna vedere come si comporta (forse va aggiunto nel commit il file ROOT.war.dodeploy per effettuare il rideploy a caldo)


https://openshift.redhat.com/community/blogs/new-openshift-release-june-26-2012-jboss-eap-hot-deployments-pricing-and-more


Hot Deployment

Hot deployment is slowly being added. We've started with the Jboss AS 7 cartridge, but will be implementing the same feature with the others soon. Hot deployment allows you to push to your application without having to restart it. This helps with zero downtime deployments. To use it add the following marker to your git repo:
.openshift/markers/hot_deploy
Just git add, commit and push it and we'll attempt to deploy the new code without stopping JBoss. Keep in mind you still have to live inside your memory footprint. 
So, if you have a memory intensive app that has a memory intensive build, you may want to look at using Jenkins to build so the process happens inside a different gear.