Handler error while trying to execute Jetty Server - java

I get error: incompatible types: org.eclipse.jetty.servlet.ServletHandler cannot be converted to org.mortbay.jetty.Handler
While trying to run my below code. I'm new to Java and not sure why this is happening. Any ideas? (I'm using JDK 11 and the latest Jetty versions 9.3 and IDE IntelliJ)
package newJetty;
import newJetty.handler.PingHandler;
import org.eclipse.jetty.servlet.ServletHandler;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
/**
* Hello world!
*
*/
public class JettyServer
{
public static void main( String[] args ) throws Exception {
Server server = new Server(8080);
ServletHandler handler = new ServletHandler();
handler.addServletWithMapping(PingHandler.class, "/ping");
server.setHandler(handler);
//
server.start();
server.join();
}
}

You are importing the wrong classes.
Remove the imports:
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
and change to the following imports:
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.Handler;

Related

Eclipse - Drools - Exception in thread "main" java.lang.ExceptionInInitializerError

I am trying to run a Drools app, my ecplise got corrupted, I reinstalled, reloaded drools, jbpm, maven and I can not figure out why I get this error in every drools app I run. Even working demos from github
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.drools.compiler.rule.builder.dialect.asm.InvokerGenerator.createStubGenerator(InvokerGenerator.java:34)
Sample Code: (Confirmed worked before I had to reinstall)
package com.jenn.DroolsDemo;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import org.drools.compiler.compiler.DroolsParserException;
import org.drools.compiler.compiler.PackageBuilder;
import org.drools.compiler.rule.builder.dialect.*;
import org.drools.core.RuleBase;
import org.drools.core.RuleBaseFactory;
import org.drools.core.WorkingMemory;
/**
*
* #author Binod Suman
* Binod Suman Academy YouTube
*
*/
public class DemoTest {
public static void main(String[] args) throws DroolsParserException, IOException {
DemoTest client = new DemoTest();
client.execteRule();
}
public void execteRule() throws DroolsParserException, IOException{
PackageBuilder builder = new PackageBuilder();
String ruleFile = "/offers.drl";
InputStream resourceAsStream = getClass().getResourceAsStream(ruleFile);
Reader ruleReader = new InputStreamReader(resourceAsStream);
builder.addPackageFromDrl(ruleReader);
org.drools.core.rule.Package rulePackage = builder.getPackage();
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage(rulePackage);
WorkingMemory workingMemory = ruleBase.newStatefulSession();
PaymentOffer paymentOffer = new PaymentOffer();
paymentOffer.setChannel("paytm");
workingMemory.insert(paymentOffer);
workingMemory.fireAllRules();
System.out.println("The cashback for this payment channel "+paymentOffer.getChannel()+" is "+paymentOffer.getDiscount());
}
}
After some research and the post above from Roddy it is confirmed my development partner is using very old drools core. I am working to get that upgraded in the code.

Deploy a pod using Java Kubernetes client Api

What is the clean way to deploy a pod using kubernetes client api in Java ?
import io.kubernetes.client.ApiClient;
import io.kubernetes.client.ApiClient;
import io.kubernetes.client.ApiException;
import io.kubernetes.client.Configuration;
import io.kubernetes.client.apis.CoreV1Api;
import io.kubernetes.client.models.V1Pod;
import io.kubernetes.client.models.V1PodList;
import io.kubernetes.client.util.Config;
import java.io.IOException;
public class Example {
public static void main(String[] args) throws IOException, ApiException{
ApiClient client = Config.defaultClient();
Configuration.setDefaultApiClient(client);
CoreV1Api api = new CoreV1Api();
V1Pod podTemplate = init_pod;
V1Pod pod = api.createNamespacedPod(pod creation arguments and podTemplate)
System.out.println("pod status : " + pod.getStatus().getPhase());
}
}
The above code might not be accurate. But this code might give you a gist of getting started.
A sample medium post that describes using java client of kubernetes is here

Access log is empty with Grizzly server and Jersey

I am unable to make Grizzly server write an access log.
The simplest setup is as follows:
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.server.accesslog.AccessLogBuilder;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import java.net.URI;
import java.util.HashMap;
public class App {
public static void main(String[] args) throws Exception {
URI uri = new URI("http://localhost:12987/");
ResourceConfig rc = new ResourceConfig().registerClasses(Greeter.class);
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(uri, rc);
new AccessLogBuilder("hi.access.log").instrument(server.getServerConfiguration());
server.start();
}
}
Code of resource:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
#Path("/")
public class Greeter {
#GET
#Path("hi")
public String hi() { return "hi!"; }
}
Gradle script containing dependency descriptions and versions:
buildscript {
ext.java_version = '1.8'
ext.jersey_version = '2.25.1'
repositories {
mavenCentral()
}
}
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "javax.servlet:servlet-api:2.5"
compile "org.glassfish.jersey.core:jersey-server:$jersey_version"
compile "org.glassfish.jersey.containers:jersey-container-servlet-core:$jersey_version"
compile "org.glassfish.jersey.containers:jersey-container-grizzly2-http:$jersey_version"
}
I observe an unexpected behavior where the access-log file is created with the start of the server but nothing is written to it when requests are made. The server sends responses and works fine in every other aspect.
I was trying to debug the thing and did not help because Jersey relies on tons of reflection and dynamic loading. I was also trying to add properties for monitoring, specifically ServerProperties.MONITORING_ENABLED but again that did not change anything.
What should I add or configure to get access log working?
It turns out that the only needed change is
HttpServer server = GrizzlyHttpServerFactory.createHttpServer(uri, rc, false);
Note the third arguments false which tells the factory not to start server immediately. Otherwise any configurations of the server (after it has been started) do not affect the behavior.

unresolved compilation using hbase in java

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
Import org.apache.hadoop.hbase.client.HBaseAdmin;
public class HBaseConnection
{
public static void main(String[] args) throws IOException
{
HBaseConfiguration hc = new HBaseConfiguration(new Configuration());
HTableDescriptor ht = new HTableDescriptor("guru99");
ht.addFamily( new HColumnDescriptor("education"));
ht.addFamily( new HColumnDescriptor("projects"));
System.out.println( "connecting" );
HBaseAdmin hba = new HBaseAdmin( hc );
System.out.println( "Creating Table" );
hba.createTable( ht );
System.out.println("Done......");
}
}
The above is my java code that I'm using to connect my hbase with my java api, but I get an error as
Exception in thread "main" java.lang.Error: Unresolved compilation
error
I cleaned the project and tried running it again, I have added all the external jar files that HBase has, by the im using HBase in a pseudo distribution mode with hadoop, and at the top of my eclipse I also get an error as
The type com.google.com.protobuf.GeneratedMessage$Builder cannot be resolved. It is indirectly referenced from required .class files

Netty: Bound Mismatch

I am new to java and was trying to use Netty to build a sample tcp server. Here is what i have currently
package http_server;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import netty_tutorial.EchoServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
class server
{
ServerBootstrap bootstrap;
int port;
server(int port_)
{
port = port_;
bootstrap = new ServerBootstrap();
bootstrap.group(new NioEventLoopGroup());
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.localAddress(new InetSocketAddress(port));
/**
* Add handlers using anonymous class
*/
/****PROBLEMATIC LINE*****/
bootstrap.childHandler(new ChannelInitializer<SocketChannel>()
{
#Override
protected void initChannel(SocketChannel ch) throws Exception {
// TODO Auto-generated method stub
System.out.println("hello");
}
}
);
}
}
public class simple_server
{
public static void main(String args[])
{
server server_obj = new server(8080);
}
}
I was planning to add my handlers inside the initChannel method, but somehow i am not able to compile the current program. As soon as i try to compile this sample program, i get the following error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Bound mismatch: The type SocketChannel is not a valid substitute for the bounded parameter <C extends Channel> of the type ChannelInitializer<C>
at http_server.server.<init>
at http_server.simple_server.main
Any idea on what exactly is going wrong?
You have imported the SocketChannel from the wrong package.
Replace import java.nio.channels.SocketChannel;
with import io.netty.channel.socket.SocketChannel;

Categories