Connect KafkaProducer using Servlet - apache-kafka

I am trying to connect KafkaProducer using servlet and setup my project in eclipse. But when i run KafkaProducer i get an exception:
org.apache.catalina.core.StandardWrapperValve invoke SEVERE:
Servlet.service() for servlet [simple_kafka.SimpleProducer] in context
with path [/kafka_web] threw exception [Servlet execution threw an
exception] with root cause java.lang.ClassNotFoundException:
org.apache.kafka.clients.producer.KafkaProducer at
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1291)
at
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119)
at simple_kafka.SimpleProducer.doPost(SimpleProducer.java:56) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:661) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:742) at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
at
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
my KafkaProducerServlet is as follow:
package simple_kafka;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
//import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
/**
* Servlet implementation class SimpleProducer
*/
#WebServlet("/producer")
public class SimpleProducer extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public SimpleProducer() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* #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);
response.getWriter().append("Served at: ").append(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG);
String topicName = "ReplicaTopic";
String key = "Key1";
String value = "Value-1";
Properties props = new Properties();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.1.59:9092,192.168.1.59:9093");
props.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer<String, String> producer = new KafkaProducer <String, String>(props);
try {
ProducerRecord<String, String> record = new ProducerRecord<>(topicName,key,value);
producer.send(record);
producer.close();
}
catch(Exception e) {
response.getWriter().append("Exception at: ").append((CharSequence) e);
}
// response.sendRedirect("/consumer");
}
}
I had Included Following JAR's in BuildPath of project Property:
Kafka 2.12-0.11.0.1.jar
kafka-clients-0.11.0.1.jar
slf4j-api-1.7.25.jar

After day long searching i found that you need to include the JAR files WEB-INF-> lib folder and you need to delete your server from eclipse & reconfigured it. After doing that i am able to run the KafkaProducer.

Related

Kafka streams NoClassDefFoundError ByteBufferInputStream

I am trying the following Kafka stream sample code.
http://kafka.apache.org/0110/documentation/streams/tutorial
import java.io.*;
import java.net.URL;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import org.json.JSONObject;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.kstream.KStreamBuilder;
#WebServlet("/KafkaStreamsTest")
public class KafkaStreamsTest extends HttpServlet {
/**
*
*/
private final String topic = "testtopic";
private final String streamouttopic = "streamtesttopic";
public static final String CLIENT_ID = "SampleStreamProducer";
JSONObject jsonObject;
#Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
}
// Do the same thing for GET and POST requests
#Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
System.out.println("Server called");
Properties props = new Properties();
props.put("bootstrap.servers", "localhost");
props.put("group.id", CLIENT_ID);
props.put(StreamsConfig.APPLICATION_ID_CONFIG, CLIENT_ID);
props.put(StreamsConfig.KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
props.put(StreamsConfig.VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());
KStreamBuilder builder = new KStreamBuilder();
builder.stream(topic).to(streamouttopic);
final KafkaStreams streams = new KafkaStreams(builder, props);
final CountDownLatch latch = new CountDownLatch(1);
// attach shutdown handler to catch control-c
Runtime.getRuntime().addShutdownHook(new Thread("streams-shutdown-hook") {
#Override
public void run() {
streams.close();
latch.countDown();
}
});
try {
streams.start();
latch.await();
} catch (Throwable e) {
System.exit(1);
}
System.exit(0);
}
}
When I run this, I am getting error as
Exception in thread "StreamThread-1" java.lang.NoClassDefFoundError: org/apache/kafka/common/record/ByteBufferInputStream
Could someone guide me what could the reason? I have kafka-clients-0.11.0.0-cp1.jar, kafka-streams-0.10.0.0.jar and other required JAR files included in this Eclipse project.

Connection error to postgresql via servlets

I am new to postgresql and JDBC.I am not able to figure out my mistake.I have filled in the port number,password and username correctly.
My Code:
package ieiPackage;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
/**
* Servlet implementation class login1
*/
#WebServlet("/login1")
public class login1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public login1() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("-------- PostgreSQL "
+ "JDBC Connection Testing ------------");
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your PostgreSQL JDBC Driver? "
+ "Include in your library path!");
e.printStackTrace();
return;
}
System.out.println("PostgreSQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/members","xxxx","xxxx");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
The Error in the console:
-------- PostgreSQL JDBC Connection Testing ------------
Where is your PostgreSQL JDBC Driver? Include in your library path!
java.lang.ClassNotFoundException: org.postgresql.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at ieiPackage.login1.doGet(login1.java:36)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
The problem is that postgresql driver cannot be found in classpath at runtime.
In orde to solve it:
a) if you use Maven add the following to pom.xml
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
b) Otherwise just download the driver from link and place it into WEB-INF/lib folder
My guess would be that your application cannot find the postgresql jdbc driver. Is it in your class path when executing?

Run servlet on glassfish server prompts for master password and run fails

I have written a simple HelloServlet as follows.
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HelloServlet
*/
#WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public HelloServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>Hello</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1>Hello</H1>\n" +
"</BODY></HTML>");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
I have GlassFish 3.1 installed in the Java EE perspective in Eclipse.
Whenever I try and run the servlet clicking on "run on server" on the server it comes up with the following message:
This domain requires an administrative password to be set before the
domain can be started. Please specify an administrative password. The
Master Password is required to start the domain. No console, no
prompting possible. You should either create the domain with
--savemasterpassword=true or provide a password file with the --passwordfile option.
Command start-domain failed.
There is always a default master password "changeit" for glassfish. Why is it prompting me with this message ?

java.lang.ClassNotFoundException: org.hibernate.criterion.Projection

Good Morning, please i don't know how to resolve that exception, i'm using ApcheTomcat 7 under eclipse and that exception is thrown everytime i run my servlet, here's my servlet for more clarity:
package servlets;
import java.util.*;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import dao.*;
#WebServlet("/AdminServlt")
public class AdminServlt extends HttpServlet {
private static final long serialVersionUID = 1L;
CarHome carDao;
public void init(ServletConfig config) throws ServletException{
carDao = new CarHome();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
populateAdminView(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
public void populateAdminView(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
ArrayList productsList = (ArrayList) carDao.extractAdminAttributes();
HttpSession session = request.getSession(true);
session.setAttribute("product_list",productsList);
RequestDispatcher rd = request.getRequestDispatcher("Admin.jsp");
rd.forward(request, response);
}
}
and here's CarHome.java
package dao;
import java.util.ArrayList;
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.*;
import entity.*;
public class CarHome {
private static final Log log = LogFactory.getLog(CarHome.class);
private final SessionFactory sessionFactory = getSessionFactory();
public List extractAdminAttributes() {
log.debug("Extracting car attributes for administrator view");
try {
ArrayList results = (ArrayList) sessionFactory
.getCurrentSession()
.createCriteria("pojo.Car")
.setProjection(
Projections.projectionList()
.add(Projections.property("carDesc"))
.add(Projections.property("price"))
.add(Projections.property("quantity")));
return results;
} catch (RuntimeException re) {
log.error("find by admin view failed");
throw re;
}
}
}
here's the stacktrace of the exception
java.lang.ClassNotFoundException: org.hibernate.criterion.Projection
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
servlets.AdminServlt.init(AdminServlt.java:31)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
i have solved my problem it was very simple, the main reason is that the hibernate.cor.jar file wasn't in the lib folder under the web-content of the application when i put it there it works fine
I was facing the same issue but in my case i re-ordered the hibernate-cor.jar on the top using eclipse
chose the project --> right click -->build path -->order tab -->chose the hibernate-cor.jar click top

eclipse delete servlet with web.xml entries

am using Eclipse's New->Servlet wizard thereby having auto-generated servlet and servlet-mapping entries ready for me. But when I select a servlet's java file and delete it, the corresponding entries in web.xml don't get deleted.
How do I do this?
I would imagine just edit the web.xml file and delete the entries. It's likely by design that they are not automatically deleted when you delete the servlet.
How about using Annotations? You don't have to take care of any configuration in web.xml for this.
package com.inventwheel.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class DeleteMe
*/
#WebServlet(description = "DeleteMe Servlet", urlPatterns = { "/DeleteMe" })
public class DeleteMe extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public DeleteMe() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}