How to get output of docker executed command in docker-java library - java

I am trying to pull an alpine image using docker-java library. Image got pulled successfully but I am not seeing any docker output on console. How to enable logging in docker-java library.
public class DockerPull {
public static void main(String[] args) throws InterruptedException {
DockerClientConfig standard = DefaultDockerClientConfig.createDefaultConfigBuilder().build();
DockerClient dockerClient = DockerClientBuilder.getInstance(standard).build();
dockerClient.pullImageCmd("alpine:latest").exec(new PullImageResultCallback()).awaitCompletion(3600, TimeUnit.SECONDS);
}
}
Output:
Process finished with exit code 0

Related

Java-grpc and tikv-java: NoSuchFieldError: CONTEXT_SPAN_KEY

I am using java-grpc together with tikv-java (separately they work OK). But together I am struggling with the following error:
Exception in thread "main" java.lang.NoSuchFieldError: CONTEXT_SPAN_KEY
at io.grpc.internal.CensusTracingModule$TracingClientInterceptor.interceptCall(CensusTracingModule.java:327)
at io.grpc.ClientInterceptors$InterceptorChannel.newCall(ClientInterceptors.java:104)
at io.grpc.internal.ManagedChannelImpl.newCall(ManagedChannelImpl.java:551)
at io.grpc.stub.ClientCalls.blockingUnaryCall(ClientCalls.java:113)
at com.pv.app.GetInsertServiceGrpc$GetInsertServiceBlockingStub.insert(GetInsertServiceGrpc.java:195)
at com.pv.app.Client.main(Client.java:55)
My code-client:
package com.pv.app;
import io.grpc.*;
public class Client {
public static void main(String[] args) throws Exception {
// Channel is the abstraction to connect to a service endpoint
// Let's use plaintext communication because we don't have certs
final ManagedChannel channel =
ManagedChannelBuilder.forTarget("0.0.0.0:8080").usePlaintext().build();
GetInsertServiceGrpc.GetInsertServiceBlockingStub stub =
GetInsertServiceGrpc.newBlockingStub(channel);
GetInsertServiceOuterClass.HelloMessage request =
GetInsertServiceOuterClass.HelloMessage.newBuilder().setName("hello").build();
System.out.println(request);
System.out.println("b4 req");
// Finally, make the call using the stub
stub.insert(request);
channel.shutdownNow();
}
}
My code-server:
package com.pv.app;
import io.grpc.Server;
import io.grpc.ServerBuilder;
/** Hello world! */
public class App {
public static void main(String[] args) throws Exception {
System.out.println("Hello-start");
Server server = ServerBuilder.forPort(8080).addService(new GetInsertServiceImpl()).build();
// Start the server
server.start();
// Server threads are running in the background.
System.out.println("Server started");
// Don't exit the main thread. Wait until server is terminated.
server.awaitTermination();
}
}
My code-implementation:
package com.pv.app;
import org.tikv.common.TiConfiguration;
import org.tikv.common.TiSession;
import org.tikv.raw.RawKVClient;
public class GetInsertServiceImpl
extends GetInsertServiceGrpc.GetInsertServiceImplBase {
#Override
public void insert(
GetInsertServiceOuterClass.HelloMessage request,
io.grpc.stub.StreamObserver<com.google.protobuf.Empty> responseObserver) {
// HelloRequest has toString auto-generated.
System.out.println("insert");
System.out.println(request);
TiConfiguration conf = TiConfiguration.createRawDefault("pd0:2379");
System.out.println(1);
System.out.println("2");
System.out.println(conf);
TiSession session = TiSession.create(conf);
System.out.println("3");
RawKVClient client = session.createRawClient();
System.out.println("4");
// When you are done, you must call onCompleted.
responseObserver.onCompleted();
}
}
My proto:
syntax = "proto3";
import "google/protobuf/empty.proto";
option java_package = "com.pv.app";
// Request payload
message HelloMessage {
string name = 1;
}
// Defining a Service, a Service can have multiple RPC operations
service GetInsertService {
// Define a RPC operation
rpc insert (HelloMessage) returns (google.protobuf.Empty) {
};
}
What I do to deploy:
In downloaded repo client-java I do mvn clean install -Dmaven.test.skip=true
In my project folder
mvn install:install-file \
-Dfile=../client-java/target/tikv-client-java-2.0-SNAPSHOT.jar \
-DgroupId=org.tikv \
-DartifactId=tikv-client-java \
-Dversion=2.0-SNAPSHOT \
-Dpackaging=jar
In my project pom.xml
<dependency>
<groupId>org.tikv</groupId>
<artifactId>tikv-client-java</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
Running java 8:
mvn -DskipTests package exec:java -Dexec.mainClass=com.pv.app.App
mvn -DskipTests package exec:java -Dexec.mainClass=com.pv.app.Client
Does anyone have a suggestion how to fix?
Full code is available here
I did my searching, tried to exclude grpc and opencensus, switch versions - did not help.
The problem is caused by conflicting io.opencensus versions. I was able to fix it by shading it in the tikv/client-java project.
In the tikv/client-java, pom.xml, maven-shade-plugin configuration:
<relocations>
...
<relocation>
<pattern>io.opencensus</pattern>
<shadedPattern>shade.io.opencensus</shadedPattern>
</relocation>
<relocations>
UPDATE
I have just realized that there were changes to the pom.xml merged to the master yesterday, so you may want to update it if you haven't yet.
UPDATE 2
I have just checked your project with the recent version of the tikv/client-java. The NoSuchFieldError: CONTEXT_SPAN_KEY is gone. There are other errors (java.net.UnknownHostException) but they are rather not related.

How do I call a #Before method from my Hooks class, in TestRunner?

I am setting up an automated framework to run tests on Android emulators, using Appium. I have added logic to launch Appium and the emulator programatically, but would like to be able to edit the "launch settings" from the TestRunner class.
My ideal goal is to have everything I need in the TestRunner class, so I can run my tests against a specific port, emulator, and tags.
But currently with the method I have now, I am receiving the following error:
'Message: cucumber.runtime.CucumberException: Hooks must declare 0 or 1 arguments.'
#CucumberOptions(
plugin = {"pretty", "html:target/cucumber-reports"}
, monochrome = true
, features = "src/test/java/feature"
, tags = "#Login"
)
public class TestRunner {
public void run() throws MalformedURLException, InterruptedException {
setUpDriver(4723, "Android9");
}
}
_________________________________________________________
public class Hooks extends DriverFactory {
static AppiumDriverLocalService service;
#Before
public static void setUpDriver(int port, String simulator) throws InterruptedException {
service = AppiumDriverLocalService
.buildService(new AppiumServiceBuilder().usingPort(port)
.usingDriverExecutable(new File("path/to/node/file"))
.withAppiumJS(new File("/path/to/appium/file")));
System.out.println("\n Appium server: " + service.getUrl());
service.start();
Thread.sleep(2000);
try {
setUpMobileDriver(simulator);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
You can pass from Maven or you can use System properties

Running docker exec in Java

I run a command from terminal and it work, the command is:
docker exec mysql-container /bin/sh -c "./build/update-time.sh 1559652300000"
I want to be able to do this in JAVA, so i have tried the following but failed. Any ideas how to make it work?
public class DockerUtils {
public static DockerComposeRule docker;
public static DockerComposeExecOption option = DockerComposeExecOption.noOptions();
public static void updateTime() {
//also tried "bin/sh", "-c"
DockerComposeExecArgument args = DockerComposeExecArgument.arguments("bash", "-c", "./build/update-time.sh 1559652300000");
//this fails
docker.exec(option, "mysql-container", args);
}
}
Throws java.lang.NullPointerException
$> docker ps
(I had to cut the output below, but there is a container with the name!)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce1e1d33dcef xx xx xx xx xx mysql-container

Java Web Service client error

I was creating a Java web service server, using eclipse IDE. that server is the following.
Note: I am working in UBUNTU
package com.tesis.service;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.RejectedExecutionException;
import com.mathworks.engine.*;
/**
* #author root
*
*/
public class CNNPredict
{
public String cNNPredict(int[] Image, int Height, int Width) throws Exception
{
String FilePath = "/home/user/Documents/MATLAB/Project";
char[] CharFilePath = FilePath.toCharArray();
MatlabEngine eng = MatlabEngine.startMatlab();
eng.feval("cd", CharFilePath);
String result = eng.feval("CNNPredict",Image,Height,Width);
return result;
}
}
As you can see I am using MATLAB engine.
Matlab engine documentation. I checked that cNNPredict method is working properly by copying it into a new Java project and It worked perfectly.
I added the .jar files required to run java engine to the Dynamic web project where the web service is located.
Apparently this web service runs without problems Web Service working in local host
If I click on "CnnPredict" link I get the wsdl direction of the class , this direction is what I use to link the client with the server.
this is the client code:
public static void main(String[] args) throws IOException, CNNPredictExceptionException
{
CNNPredictStub stub = new CNNPredictStub();
CNNPredict cnn = new CNNPredict();
BufferedImage img = null;
System.out.println("Reading image ...");
img = ImageIO.read(new File("/home/riosgamarra/Documents/MATLAB/TesisGamarrarios/101_ObjectCategories/laptop/image_0009.jpg"));
int[] UnrolledImage = convertToGray(img);
cnn.setImage(UnrolledImage);
cnn.setWidth(img.getWidth());
cnn.setHeight(img.getHeight());
System.out.println(stub.cNNPredict(cnn).get_return());
}
It has no errors, but when I run it this error message shows up:
Exception in thread "main" org.apache.axis2.AxisFault: <faultstring>com/mathworks/engine/MatlabEngine</faultstring>
at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:513)
at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:368)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:414)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:225)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:150)
at com.tesis.service.CNNPredictStub.cNNPredict(CNNPredictStub.java:197)
at com.tesis.client.CallWS.main(CallWS.java:40)
what I am missing ? do I need to add any special permissions to the server project ? What Am I missing ?
Note: I run the client clicking on the class and selecting Run as > Java application.
at com.tesis.service.CNNPredictStub.cNNPredict(CNNPredictStub.java:197)
is where the exception is but
public class CNNPredict
{
public String cNNPredict(int[] Image, int Height, int Width) throws Exception
{
String FilePath = "/home/user/Documents/MATLAB/Project";
char[] CharFilePath = FilePath.toCharArray();
MatlabEngine eng = MatlabEngine.startMatlab();
eng.feval("cd", CharFilePath);
String result = eng.feval("CNNPredict",Image,Height,Width);
return result;
}
}
is not the stub. First we need to the right code to look at. The matlab api is straight forward. My guess is that the stub is making the wrong call

Default http/admin port in dropwizard project

I have a dropwizard project and I have maintained a config.yml file at the ROOT of the project (basically at the same level as pom.xml). Here I have specified the HTTP port to be used as follows:
http:
port:9090
adminPort:9091
I have the following code in my TestService.java file
public class TestService extends Service<TestConfiguration> {
#Override
public void initialize(Bootstrap<TestConfiguration> bootstrap) {
bootstrap.setName("test");
}
#Override
public void run(TestConfiguration config, Environment env) throws Exception {
// initialize some resources here..
}
public static void main(String[] args) throws Exception {
new TestService().run(new String[] { "server" });
}
}
I expect the config.yml file to be used to determine the HTTP port. However the app always seems to start with the default ports 8080 and 8081. Also note that I am running this from eclipse.
Any insights as to what am I doing wrong here ?
Try running your service as follows:
Rewrite your main method into:
public static void main(String[] args) throws Exception {
new TestService().run(args);
}
Then in eclipse go to Run --> Run configurations...., create a new run configuration for your class, go to arguments and add "server path/to/config.yml" in "program arguments". If you put it in the root directory, it would be "server config.yml"
I believe you are not passing the location/name of the .yml file and thus your configurations are not being loaded. Another solution is to just add the location of your config file to the array you're passing into run ;)

Categories

Resources