1. Create Dynamic Web Project in Eclipse
2. Use below configuration in Tomee.xml in conf:
<Resource id="amqRA" type="ActiveMQResourceAdapter">
BrokerXmlConfig = broker:(tcp://localhost:61616)?useJmx=false
ServerUrl = vm://localhost
StartupTimeout = 10 seconds
</Resource>
<Resource id="amqCF" type="jakarta.jms.ConnectionFactory">
ResourceAdapter = amqRA
TransactionSupport = xa
PoolMaxSize = 10
PoolMinSize = 0
ConnectionMaxWaitTime = 5 seconds
ConnectionMaxIdleTime = 15 Minutes
</Resource>
3. Java Lookup code in Servlet:
package com.teg;
import java.io.IOException;
import java.io.PrintWriter;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import jakarta.jms.Connection;
import jakarta.jms.ConnectionFactory;
import jakarta.jms.JMSException;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
/**
* Servlet implementation class JndiTest
*/
public class JndiTest extends HttpServlet {
private static final long serialVersionUID = 1L;
private ConnectionFactory cf = null;
/**
* Default constructor.
*/
public JndiTest() {
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
System.out.println("Application Deployed in TomEE");
try {
InitialContext initContext = new InitialContext();
ConnectionFactory cf = (ConnectionFactory)
initContext.lookup("openejb:Resource/amqCF");
System.out.println("Connection Factory:: " + cf);
Connection connection = cf.createConnection();
connection.start();
System.out.println("Connection can be tried here");
connection.close();
} catch (NamingException | JMSException e) {
System.out.println(e);
e.printStackTrace();
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
out.println("Served at: doGet");
try {
Connection connection = cf.createConnection();
connection.start();
out.println("Connection can be tried here");
connection.close();
} catch (JMSException e) {
out.println(e);
e.printStackTrace();
}
out.println("Serviced at: doGet");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
4. Export WAR file in Put it in Tomee Webapps folder
- Please delete existing wars, folders in Webapps and Work folder before running new project.