== EAR with JMS and PgSQL == {{attachment:earFile.png}} === Setup postgresql in docker === {{{#!highlight bash cd /app/jboss-5.1.0.GA/server/default/lib wget https://jdbc.postgresql.org/download/postgresql-9.3-1103.jdbc3.jar apt install postgresql-9.5 su postgres mkdir -p /var/run/postgresql/9.5-main.pg_stat_tmp/ /usr/lib/postgresql/9.5/bin/postgres -D /var/lib/postgresql/9.5/main -c config_file=/etc/postgresql/9.5/main/postgresql.conf su postgres psql create user appuser with login password '12345678'; create database app; grant all privileges on database app to appuser; \q Datasource in Jboss , Local Tx Datasources PostgresDS org.postgresql.Driver jdbc:postgresql://localhost/app?user=appuser&password=12345678 /app/jboss-5.1.0.GA/server/default/deploy# cat PostgresDS-ds.xml PostgresDS jboss-local-jdbc.rar true javax.sql.DataSource jboss:service=invoker,type=jrmp 0 10 30000 30 false false 0 true org.jboss.resource.statistic.pool.JBossDefaultSubPoolStatisticFormatter false 0 5000 0 false false 0 0 org.postgresql.Driver jdbc:postgresql://localhost/app?user=appuser&password=12345678 }}} === Create JMS queue === {{{#!highlight bash mkdir -p /app/jboss-5.1.0.GA/server/default/deploy/queue nano /app/jboss-5.1.0.GA/server/default/deploy/queue/ContactsQueue-service.xml /queue/ContactsQueue jboss.messaging:service=ServerPeer jboss.messaging:service=PostOffice }}} === Structure === {{{ . ├── deploy_to_target.sh ├── earcontacts │ └── pom.xml │ ├── ejbcontacts │ ├── pom.xml │ └── src │ └── main │ ├── java │ │ └── org │ │ └── allowed │ │ └── bitarus │ │ ├── ContactEntity.java │ │ ├── ContactsLocal.java │ │ ├── ContactsMDB.java │ │ ├── ContactsRemote.java │ │ └── ContactsWS.java │ └── resources │ └── META-INF │ ├── ejb-jar.xml │ └── persistence.xml ├── pom.xml └── warcontacts ├── pom.xml └── src └── main ├── java │ └── org │ └── allowed │ └── bitarus │ └── Handler.java └── webapp ├── add.jsp ├── css │ ├── bootstrap.min.css │ └── warcontacts.css ├── delete.jsp ├── head.jsp ├── index.jsp ├── js │ ├── bootstrap.min.js │ └── jquery-3.5.1.min.js ├── nav.jsp ├── update.jsp ├── view.jsp └── WEB-INF └── web.xml }}} === deploy_to_target.sh === {{{#!highlight bash #!/bin/sh rm ../contacts.tgz tar cvzf ../contacts.tgz ../contacts cp ../contacts.tgz ../target/ docker exec -it jboss5_container /bin/sh -c 'cd /app;rm -rf contacts; tar xvzf contacts.tgz' docker exec -it jboss5_container /bin/sh -c 'cd /app/contacts; rm /app/jboss-5.1.0.GA/server/default/deploy/earcontacts-0.0.1.ear; mvn clean install;' docker exec -it jboss5_container /bin/sh -c 'cp /app/contacts/earcontacts/target/earcontacts-0.0.1.ear /app/jboss-5.1.0.GA/server/default/deploy' }}} === pom.xml === {{{#!highlight xml 4.0.0 org.allowed.bitarus contacts 1.0 pom ejbcontacts warcontacts earcontacts }}} === earcontacts/pom.xml === {{{#!highlight xml 4.0.0 org.allowed.bitarus earcontacts 0.0.1 ear org.allowed.bitarus warcontacts 0.0.1 war org.allowed.bitarus ejbcontacts 0.0.1 ejb org.apache.maven.plugins maven-ear-plugin 2.10.1 5 lib true org.allowed.bitarus ejbcontacts / org.allowed.bitarus warcontacts / maven-compiler-plugin 1.5 1.5 }}} === ejbcontacts/pom.xml === {{{#!highlight xml 4.0.0 org.allowed.bitarus ejbcontacts 0.0.1 ejb javaee javaee-api 5 provided maven-compiler-plugin 1.5 1.5 }}} === ejbcontacts/src/main/java/org/allowed/bitarus/ContactEntity.java === {{{#!highlight java package org.allowed.bitarus; import java.util.Date; import javax.persistence.Id; import javax.persistence.Entity; import javax.persistence.Column; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.TemporalType; import javax.persistence.Temporal; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; @Entity @NamedQueries( { @NamedQuery(name="Contacts.byId",query="select a from ContactEntity a where a.id = :id") } ) public class ContactEntity{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; @Column @Temporal(TemporalType.TIMESTAMP) private Date creationDate; @Column private String firstName; @Column private String lastName; @Column private String address; @Column private String phoneNumber; public void setId(long id){this.id=id;} public long getId(){return this.id;} public void setCreationDate(Date creationDate){this.creationDate=creationDate;} public Date getCreationDate(){return this.creationDate;} public void setFirstName(String firstName){this.firstName=firstName;} public String getFirstName(){return this.firstName;} public void setLastName(String lastName){this.lastName=lastName;} public String getLastName(){return this.lastName;} public void setAddress(String address){this.address=address;} public String getAddress(){return this.address;} public void setPhoneNumber(String phoneNumber){this.phoneNumber=phoneNumber;} public String getPhoneNumber(){return this.phoneNumber;} } }}} === ejbcontacts/src/main/java/org/allowed/bitarus/ContactsLocal.java === {{{#!highlight java package org.allowed.bitarus; import javax.ejb.Local; import java.util.List; @Local public interface ContactsLocal { void add(String firstName, String lastName, String address, String phoneNumber); List getAll(); ContactEntity get(long id); void remove(long id); void edit(long id, String firstName, String lastName, String address, String phoneNumber); void sendMessage(String message); } }}} === ejbcontacts/src/main/java/org/allowed/bitarus/ContactsMDB.java === {{{#!highlight java package org.allowed.bitarus; import javax.ejb.ActivationConfigProperty; import javax.ejb.MessageDriven; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; /* mkdir -p /app/jboss-5.1.0.GA/server/default/deploy/queue nano ContactsQueue-service.xml /queue/ContactsQueue jboss.messaging:service=ServerPeer jboss.messaging:service=PostOffice * */ @MessageDriven(mappedName = "queue/ContactsQueue", messageListenerInterface = MessageListener.class, activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"), @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/ContactsQueue") }) public class ContactsMDB implements MessageListener { public void onMessage(Message inMessage) { try { String text = ((TextMessage) inMessage).getText(); System.out.println(String.format("Got message %s", text)); } catch (Exception ex) { ex.printStackTrace(); } } } }}} === ejbcontacts/src/main/java/org/allowed/bitarus/ContactsRemote.java === {{{#!highlight java package org.allowed.bitarus; import javax.ejb.Remote; import java.util.List; @Remote public interface ContactsRemote { void add(String firstName, String lastName, String address, String phoneNumber); List getAll(); ContactEntity get(long id); void remove(long id); void edit(long id, String firstName, String lastName, String address, String phoneNumber); void sendMessage(String message); } }}} === ejbcontacts/src/main/java/org/allowed/bitarus/ContactsWS.java === {{{#!highlight java package org.allowed.bitarus; import javax.annotation.Resource; import javax.ejb.Stateless; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage; import javax.jws.WebMethod; import javax.jws.WebService; import org.allowed.bitarus.ContactsRemote; import javax.persistence.PersistenceContext; import javax.persistence.EntityManager; import java.util.List; import javax.persistence.NamedQuery; @Stateless @WebService public class ContactsWS implements ContactsRemote, ContactsLocal { @PersistenceContext(unitName = "puContactsDefaultDS") EntityManager em; @Resource(mappedName = "ConnectionFactory") private ConnectionFactory connectionFactory; @Resource(mappedName = "queue/ContactsQueue") private Queue queue; @WebMethod public void add(String firstName, String lastName, String address, String phoneNumber) { ContactEntity c = new ContactEntity(); c.setFirstName(firstName); c.setLastName(lastName); c.setAddress(address); c.setPhoneNumber(phoneNumber); c.setCreationDate(new java.util.Date()); em.persist(c); } @WebMethod public List getAll() { return em.createQuery("select a from ContactEntity a ").getResultList(); } @WebMethod public ContactEntity get(long id) { // return (ContactEntity) em.createQuery("select a from ContactEntity a where // a.id = :id").setParameter("id", id).getSingleResult(); return (ContactEntity) em.createNamedQuery("Contacts.byId").setParameter("id", id).getSingleResult(); } @WebMethod public void remove(long id) { ContactEntity ent = (ContactEntity) em.createNamedQuery("Contacts.byId").setParameter("id", id) .getSingleResult(); em.remove(ent); } @WebMethod public void edit(long id, String firstName, String lastName, String address, String phoneNumber) { ContactEntity ent = (ContactEntity) em.createNamedQuery("Contacts.byId").setParameter("id", id) .getSingleResult(); ent.setFirstName(firstName); ent.setLastName(lastName); ent.setAddress(address); ent.setPhoneNumber(phoneNumber); em.persist(ent); } @WebMethod public void sendMessage(String message) { try { Connection connection = connectionFactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer messageProducer = session.createProducer(queue); TextMessage textMessage = session.createTextMessage(); textMessage.setText(message); messageProducer.send(textMessage); messageProducer.close(); session.close(); connection.close(); } catch (Exception ex) { ex.printStackTrace(); } } } }}} === ejbcontacts/src/main/resources/META-INF/ejb-jar.xml === {{{#!highlight xml ContactsWS org.allowed.bitarus.ContactsLocal org.allowed.bitarus.ContactsRemote org.allowed.bitarus.ContactsWS Stateless Container }}} === ejbcontacts/src/main/resources/META-INF/persistence.xml === {{{#!highlight xml java:/PostgresDS }}} === warcontacts/pom.xml === {{{#!highlight xml 4.0.0 org.allowed.bitarus warcontacts 0.0.1 war javaee javaee-api 5 provided org.allowed.bitarus ejbcontacts 0.0.1 provided maven-compiler-plugin 1.5 1.5 }}} === warcontacts/src/main/java/org/allowed/bitarus/Handler.java === {{{#!highlight java package org.allowed.bitarus; import java.util.logging.Logger; import java.util.logging.Level; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.PageContext; import javax.naming.InitialContext; import java.io.IOException; import java.util.List; import javax.naming.NamingEnumeration; import javax.naming.NameClassPair; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Map; public class Handler { private static final String CONTACTS_WS_LOCAL = "earcontacts-0.0.1/ContactsWS/local"; private ServletRequest request; private ServletResponse response; private JspWriter out; public Handler(ServletRequest request, ServletResponse response, JspWriter out) { this.request = request; this.response = response; this.out = out; } public Handler(PageContext pageContext) { this.request = pageContext.getRequest(); this.response = pageContext.getResponse(); this.out = (JspWriter) pageContext.getAttribute(PageContext.OUT); HttpServletRequest hsr = (HttpServletRequest) this.request; System.out.println(String.format("Current method %s", hsr.getMethod())); System.out.println(String.format("Path %s", hsr.getServletPath())); if ("POST".equals(hsr.getMethod())) { Map params = hsr.getParameterMap(); for (String key : params.keySet()) { System.out.println(String.format("%s:%s", key, hsr.getParameter(key))); } handleAddPost(hsr); handleDeletePost(hsr); handleUpdatePost(hsr); } } private void handleUpdatePost(HttpServletRequest hsr) { if ("/update.jsp".equals(hsr.getServletPath())) { try { InitialContext context = new InitialContext(); ContactsLocal contacts = (ContactsLocal) context.lookup(CONTACTS_WS_LOCAL); long id = Integer.parseInt(hsr.getParameter("cid")); String firstName = hsr.getParameter("firstname"); String lastName = hsr.getParameter("lastname"); String address = hsr.getParameter("address"); String phoneNumber = hsr.getParameter("phonenumber"); contacts.edit(id, firstName, lastName, address, phoneNumber); redirectToIndex(hsr); } catch (Exception ex) { } } } private void handleDeletePost(HttpServletRequest hsr) { if ("/delete.jsp".equals(hsr.getServletPath())) { try { InitialContext context = new InitialContext(); ContactsLocal contacts = (ContactsLocal) context.lookup(CONTACTS_WS_LOCAL); contacts.remove(Integer.parseInt(hsr.getParameter("id"))); redirectToIndex(hsr); } catch (Exception ex) { } } } private void handleAddPost(HttpServletRequest hsr) { if ("/add.jsp".equals(hsr.getServletPath())) { try { InitialContext context = new InitialContext(); ContactsLocal contacts = (ContactsLocal) context.lookup(CONTACTS_WS_LOCAL); contacts.add(hsr.getParameter("firstname"), hsr.getParameter("lastname"), hsr.getParameter("address"), hsr.getParameter("phonenumber")); redirectToIndex(hsr); } catch (Exception ex) { } } } private void redirectToIndex(HttpServletRequest hsr) throws IOException { ((HttpServletResponse) this.response).sendRedirect(hsr.getContextPath() + "/index.jsp"); } public void writeHandler() { try { this.out.println(String.format("

Handler up %s

", this.request.getParameter("user"))); } catch (Exception ex) { } } public void writeIP() { try { this.out.print("!!! Your IP address is " + this.request.getRemoteAddr()); } catch (Exception ex) { } } public List getAllContacts() { List ret = null; try { InitialContext context = new InitialContext(); ContactsLocal contacts = (ContactsLocal) context.lookup(CONTACTS_WS_LOCAL); ret = contacts.getAll(); } catch (Exception ex) { } return ret; } public ContactEntity getContact(long id) { ContactEntity ret = null; try { InitialContext context = new InitialContext(); ContactsLocal contacts = (ContactsLocal) context.lookup(CONTACTS_WS_LOCAL); ret = contacts.get(id); } catch (Exception ex) { } return ret; } } }}} === warcontacts/src/main/webapp/add.jsp === {{{#!highlight xml <%@page import="org.allowed.bitarus.Handler, java.util.List, org.allowed.bitarus.ContactEntity" %> <% Handler h = new Handler(pageContext); %>








}}} === warcontacts/src/main/webapp/css/bootstrap.min.css === === warcontacts/src/main/webapp/css/warcontacts.css === {{{#!highlight css .button{margin-top:2px; margin-bottom:2px;} .topspace { padding-top: 10px; } 2020-06-24T18:20:45 [vitor@debian:3 ~/JBoss5Docker/contacts] }}} === warcontacts/src/main/webapp/delete.jsp === {{{#!highlight xml <%@page import="org.allowed.bitarus.Handler, java.util.List, org.allowed.bitarus.ContactEntity" %> <% Handler h = new Handler(pageContext); %> <% ContactEntity ci = h.getContact( Integer.parseInt( request.getParameter("id") ) ); %>
<% if(ci!=null) { %>

Id: <%= ci.getId() %>

First name: <%= ci.getFirstName() %>

Last name: <%= ci.getLastName() %>

Creation date: <%= ci.getCreationDate() %>

Address: <%= ci.getAddress() %>

Phone: <%= ci.getPhoneNumber() %>

<% } %>
}}} === warcontacts/src/main/webapp/head.jsp === {{{#!highlight xml }}} === warcontacts/src/main/webapp/index.jsp === {{{#!highlight xml <%@page import="org.allowed.bitarus.Handler, java.util.List, org.allowed.bitarus.ContactEntity" %> <% Handler h = new Handler(pageContext); %>
<% List contacts = h.getAllContacts(); %> Add <% for(int i=0; i <% ContactEntity contact = ((ContactEntity)contacts.get(i)); %> <%}%>
FirstName LastName Actions
<%= contact.getFirstName() %> <%= contact.getLastName() %> View Update Delete
}}} === warcontacts/src/main/webapp/js/bootstrap.min.js === === warcontacts/src/main/webapp/js/jquery-3.5.1.min.js === === warcontacts/src/main/webapp/nav.jsp === {{{#!highlight xml }}} === warcontacts/src/main/webapp/update.jsp === {{{#!highlight xml <%@page import="org.allowed.bitarus.Handler, java.util.List, org.allowed.bitarus.ContactEntity" %> <% Handler h = new Handler(pageContext); %>
<% if( request.getParameter("id") != null ) { %> <% ContactEntity ci = h.getContact( Integer.parseInt( request.getParameter("id") ) ); %>








<% } %>
}}} === warcontacts/src/main/webapp/view.jsp === {{{#!highlight xml <%@page import="org.allowed.bitarus.Handler, java.util.List, org.allowed.bitarus.ContactEntity" %> <% Handler h = new Handler(pageContext); %>
<% ContactEntity ci = h.getContact( Integer.parseInt( request.getParameter("id") ) ); %>

Id: <%= ci.getId() %>

First name: <%= ci.getFirstName() %>

Last name: <%= ci.getLastName() %>

Creation date: <%= ci.getCreationDate() %>

Address: <%= ci.getAddress() %>

Phone: <%= ci.getPhoneNumber() %>

}}} === warcontacts/src/main/webapp/WEB-INF/web.xml === {{{#!highlight xml }}}