Unable to get service details on port? - java

I wish to get the service name being used in the port. However, I am unable to. What I want to do is to check if a port is used. If it is used, then I want to get the service details on that port. How can I achieve this and what I am doing wrong?
public int checkPort(int port){
try {
InetAddress inetAddress = InetAddress.getLocalHost();
ss = new Socket(inetAddress.getHostAddress(), port);
if(ss.isBound()) {
System.out.println("Port " + port + " is being used by ");
Process p1 = Runtime.getRuntime().exec("grep -w " + port + " /etc/services");
p1.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p1.getInputStream()));
String line = reader.readLine();
while(line != null) {
System.out.println(line);
line = reader.readLine();
}
}
ss.close();
} catch (Exception e) {
System.out.println("Port " +port+ " is not being used");
}
return 0;
}
Results in
Port 139 is being used by
Port 139 is not being used

Well, assuming you are on Windows (it may or may not be different on other operating systems), you may be getting this exception.
Cannot run program "grep": CreateProcess error=2, The system cannot find the file specified
At least, that is what I got. You might be getting a totally different error. The main issue here is that there is one big try catch block with no e.printStackTrace() and it catches every exception. This means that when it goes wrong, there is no way to know why.
Hopefully this will work for you. Ironically you do not need a socket to test for services on a port so this may be an XY problem.
My solution to finding services on a port is the following.
SocketTester.java
package socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeSet;
/**
* An answer for Unable to get service details on port?
*
* #see Unable to get service details on port?
* #version 1.0
* #author Dan
*/
public class SocketTester {
/**
* This method checks whether a port is being used by any services.
* It will output any information to the system console.
*
* #param port The port to be checked for any services
*/
public static void checkPort(int port) {
TreeSet<String> pids = null;
List<Service> services = null;
pids = getPIDs(port);
if(pids != null) {
services = getServices(port, pids);
}
listInformation(port, services);
}
/**
* This method checks whether there are any PIDs on the specified port.
* If there are these are then returned.
*
* #param port The port to check for PIDs
* #return It returns a TreeSet containing any found PIDs on the specified port
*/
private static TreeSet<String> getPIDs(int port) {
TreeSet<String> returnVal = new TreeSet<String>();
ProcessBuilder pidProcessBuilder = new ProcessBuilder("cmd.exe", "/C", "netstat -ano | find \"" + port + "\"");
pidProcessBuilder.redirectErrorStream(true);
Process pidProcess = null;
try {
pidProcess = pidProcessBuilder.start();
} catch (IOException e) {
return null;
}
BufferedReader pidProcessOutputReader = new BufferedReader(new InputStreamReader(pidProcess.getInputStream()));
String outputLine = null;
try {
outputLine = pidProcessOutputReader.readLine();
} catch (IOException e) {
return null;
}
while (outputLine != null) {
List<String> outputLineParts = new ArrayList<String>(Arrays.asList(outputLine.split(" ")));
outputLineParts.removeAll(Arrays.asList(""));
//outputLineParts.get(1) is the local address. We don't want a foreign address to accidently be found
//outputLineParts.size() - 1 is the PID
if(outputLineParts.get(1).contains(":" + port) && !returnVal.contains(outputLineParts.get(outputLineParts.size() - 1))) {
returnVal.add(outputLineParts.get(outputLineParts.size() - 1));
}
try {
outputLine = pidProcessOutputReader.readLine();
} catch (IOException e) {
return null;
}
}
try {
pidProcess.waitFor();
} catch (InterruptedException e) {
return null;
}
return returnVal;
}
/**
* This method checks whether there are any services related to the PID.
* If there are these are then returned.
*
* #param port A reference to the PIDs port
* #param pids A list of PIDs found by getPIDs
* #return It returns a List containing any found services on the specified PIDs
*/
private static List<Service> getServices(int port, TreeSet<String> pids) {
List<Service> returnVal = new ArrayList<Service>();
for(String pid : pids) {
ProcessBuilder serviceProcessBuilder = new ProcessBuilder("cmd.exe", "/C", "tasklist /svc /FI \"PID eq " + pid + "\" | find \"" + pid + "\"");
serviceProcessBuilder.redirectErrorStream(true);
Process serviceProcess = null;
try {
serviceProcess = serviceProcessBuilder.start();
} catch (IOException e) {
return null;
}
BufferedReader serviceProcessOutputReader = new BufferedReader(new InputStreamReader(serviceProcess.getInputStream()));
String outputLine = null;
try {
outputLine = serviceProcessOutputReader.readLine();
} catch (IOException e) {
return null;
}
while(outputLine != null) {
List<String> outputLineParts = new ArrayList<String>(Arrays.asList(outputLine.split(" ")));
outputLineParts.removeAll(Arrays.asList(""));
//outputLineParts.get(0) is the service
returnVal.add(new Service(port, pid, outputLineParts.get(0)));
try {
outputLine = serviceProcessOutputReader.readLine();
} catch (IOException e) {
return null;
}
}
try {
serviceProcess.waitFor();
} catch (InterruptedException e) {
return null;
}
}
return returnVal;
}
/**
* This method lists the information found by checkPort
*
* #param port The port that has been checked for services
* #param servicesRunning The services found on the port
*/
private static void listInformation(int port, List<Service> servicesRunning) {
if(servicesRunning != null && servicesRunning.size() != 0) {
System.out.println("The following services are being run on port " + port);
for(Service service : servicesRunning) {
System.out.println("\t" + service.getService());
}
} else {
System.out.println("There are no services being run on port " + port);
}
}
public static void main(String[] args) {
final int portToCheck = 135;
checkPort(portToCheck);
}
}
Sevice.java
package socket;
/**
* An supplementary class to support SocketTester
*
* #see Unable to get service details on port?
* #version 1.0
* #author Dan
*/
public class Service {
private int port;
private String pid;
private String service;
public Service(int port, String pid, String service) {
this.port = port;
this.pid = pid;
this.service = service;
}
public int getPort() {
return port;
}
public String getPID() {
return pid;
}
public String getService() {
return service;
}
#Override
public String toString() {
return "Service \"" + "\" is being run on port " + port + " and has the PID " + pid;
}
}

Related

Having trouble with Java client-server communication, where the out.println seems to be delayed?

I'm confused as to why I cant seem to get the server to output to the client properly. I'm not the most experienced when it comes to java and have exhausted anything I could think of. The other systems in place seem to work fine(The Add/List commands).
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.tcpechoclient;
import java.io.*;
import java.net.*;
/**
*
* #author Leepe
*/
public class TCPEchoClient {
private static InetAddress host;
private static final int PORT = 1248;
public static void main(String[] args) {
try
{
host = InetAddress.getLocalHost();
}
catch(UnknownHostException e)
{
System.out.println("Host ID not found!");
System.exit(1);
}
run();
}
private static void run() {
Socket link = null; //Step 1.
try
{
link = new Socket(host,PORT); //Step 1.
//link = new Socket( "192.168.0.59", PORT);
BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream()));//Step 2.
PrintWriter out = new PrintWriter(link.getOutputStream(),true); //Step 2.
//Set up stream for keyboard entry...
BufferedReader userEntry =new BufferedReader(new InputStreamReader(System.in));
String message = "";
String response = "";
while (!message.equals("Stop")) {
System.out.println("Enter message to be sent to server: ");
message = userEntry.readLine();
out.println(message); //Step 3.
// out.flush();
response = in.readLine(); //Step 3.
System.out.println("\nSERVER RESPONSE> " + response);
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
System.out.println("\n* Closing connection... *");
link.close(); //Step 4.
}catch(IOException e)
{
System.out.println("Unable to disconnect/close!");
System.exit(1);
}
}
} // finish run method
} //finish the class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.tcpechoserverthreads;
import java.io.*;
import java.net.*;
/**
*
* #author Leepe
*/
public class TCPEchoServer {
private static ServerSocket servSock;
private static final int PORT = 1248;
private static int clientConnections = 0;
public static void main(String[] args) {
System.out.println("Opening port..."+"\n"+"Listening on port: "+PORT);
try
{
servSock = new ServerSocket(PORT); //Step 1.
}
catch(IOException e)
{
System.out.println("Unable to attach to port!");
System.exit(1);
}
do
{
run();
}while (true);
}
synchronized private static void run()
{
Socket link = null; //Step 2.
try
{
link = servSock.accept();
clientConnections++;
String client_ID = clientConnections + "";
Runnable resource = new ClientConnectionRun(link, client_ID);
Thread t = new Thread (resource);
t.start();
}
catch(IOException e1)
{
e1.printStackTrace();
try {
System.out.println("\n* Closing connection... *");
link.close(); //Step 5.
}
catch(IOException e2)
{
System.out.println("Unable to disconnect!");
System.exit(1);
}
}
} // finish run method
} // finish the class
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.tcpechoserverthreads;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.*;
/**
*
* #author Leepe
*/
public class ClientConnectionRun implements Runnable {
Socket client_link = null;
String clientID;
public static List<String> currentList = new ArrayList<String>();
String[] parts;
String part1;
String part2;
String part3;
String message = "";
public ClientConnectionRun(Socket connection, String cID) {
this.client_link = connection;
clientID = cID;
}
#Override
synchronized public void run() {
try{
BufferedReader in = new BufferedReader( new InputStreamReader(client_link.getInputStream())); //Step 3.
PrintWriter out = new PrintWriter(client_link.getOutputStream(),true); //Step 3.
System.out.println("\n* started connection with the client " + clientID + " ... *");
while (!message.equals("Stop")){//Step 4.
message = in.readLine();
// out.flush();
System.out.println("\n Message received from client: " + clientID + " - "+ message);
out.println("\n Echo Message: " + message);
//out.flush();
if(message.contains(";")){
// String userinput = message;
// String item = message;
// System.out.println("Contains ;");
String[] parts = message.split(";");
String part1 = parts[0];
String part2 = parts[1];
System.out.println(part1);
System.out.println(part2);
if(parts.length >= 3 && part1.equals("add")){
String part3 = parts[2];
System.out.println(part1);
System.out.println(part2);
System.out.println(part3);
currentList.add(part2+" - "+part3);
//AddItem();
}
else if(parts.length <= 2 && part1.equals("list") ){
System.out.println("list command working");
//ListItem();
System.out.println();
System.out.println("----------------------");
System.out.println("To-Do List");
System.out.println("----------------------");
int number = 0;
for (Iterator<String> it = currentList.iterator(); it.hasNext();) {
message = it.next();
if(message.contains(part2)){
System.out.println(++number + " " + message);
}
}
System.out.println("----------------------");
}
else {
System.out.println("\n Don't add a description if you are searching for a date");
}
}
else if(!message.contains(";")){
System.out.println("\n Unknown command, Try using 'add' or 'list' followed by a ' ; ' as listed above ");
}
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try {
System.out.println("\n* Closing connection with the client " + clientID + " ... *");
client_link.close(); //Step 5.
}
catch(IOException e)
{
System.out.println("Unable to disconnect!");
}
}
}
}
I want the server to be able to output info to the client that's connected. What's happening is that the server will output the info eventually but requires me to enter several more inputs, i'm not sure how its delayed and any help would be appreciated.
Leaving everything else untouched, simply changing your line out.println("\nEcho Message: " + message); in your Class ClientConnectionRun to out.println("Echo Message: " + message); will fix this.
Essentially, what goes wrong is that your line response = in.readLine(); in the client terminates when it encounters a line-feed. So, if you begin your response with a line-feed, it will terminate instantly and is thus always trailing by one line, which is why you will only see the actual response the next time you enter some input.
From the doc of java.io.BufferedReader.readLine():
Reads a line of text. A line is considered to be terminated by any one
of a line feed ('\n'), a carriage return ('\r'), a carriage return
followed immediately by a line feed, or by reaching the end-of-file
(EOF).
Also, some general input:
You can drastically improve your code by using try-with-resources statements (since Java 8). This way you can avoid the "nasty" nested try-catch-finally constructs to handle your streams. For your Client for example you could simplify do:
try (Socket link = new Socket(host, PORT);
BufferedReader in = new BufferedReader(new InputStreamReader(link.getInputStream()));
PrintWriter out = new PrintWriter(link.getOutputStream(), true);
BufferedReader userEntry = new BufferedReader(new InputStreamReader(System.in))) {
And all you need at the end is this:
} catch (IOException e) {
e.printStackTrace();
}

consume all messages from IBM MQ

I want to consume all messages from MQ.
public static void main(String[] args)
{
JMSContext context = null;
Destination destination = null;
JMSConsumer consumer = null;
JmsFactoryFactory FF = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
JmsConnectionFactor CF = FF.createConnectionFactory();
context = CF.createContext();
destination = context.createQueue(QUEUE_NAME);
consumer = context.createConsumer(destination);
String msg = consumer.receiveBody(String.class, 15090);
System.out.println(msg);
}
It is able to read one message only. How can I consume all messages? Also, is there any simpler way to delete all messages in queue without even reading or consuming them?
The JMS API consumes a single message at a time so you'll need to put your receiveBody in a loop, e.g.:
public static void main(String[] args) {
JMSContext context = null;
Destination destination = null;
JMSConsumer consumer = null;
JmsFactoryFactory FF = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
JmsConnectionFactor CF = FF.createConnectionFactory();
context = CF.createContext();
destination = context.createQueue(QUEUE_NAME);
consumer = context.createConsumer(destination);
String msg = null;
do {
msg = consumer.receiveBody(String.class, 15090);
System.out.println(msg);
} while (msg != null);
}
When receiveBody returns null that means there's no more messages in the queue.
The JMS API doesn't define any way to delete all the messages from a queue, but most JMS servers have an implementation-specific management API through which you can perform those sorts of actions.
If, as your question suggests, all you want to do is empty a queue of all its messages and not actually read them in an application, you could consider simply using the administrative MQSC command:-
CLEAR QLOCAL(queue-name)
You can type this into the runmqsc tool to issue it to the queue manager.
is there any simpler way to delete all messages in queue without even
reading or consuming them?
Yes. You can use the MQ PCF Clear Queue command, so long as no application has the queue open for input or output. i.e. IPPROCS and OPPROCS must be zero for it to work. This is also true for Morag's MQSC Clear command.
Here is a fully functioning Java MQ PCF program to clear a queue.
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import com.ibm.mq.MQException;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.CMQC;
import com.ibm.mq.constants.CMQCFC;
import com.ibm.mq.headers.MQDataException;
import com.ibm.mq.headers.pcf.PCFMessage;
import com.ibm.mq.headers.pcf.PCFMessageAgent;
/**
* Program Name
* MQClearQueue01
*
* Description
* This java class issues a PCF "Clear Q" command for a queue to delete all messages
* in the queue of a remote queue manager.
*
* Sample Command Line Parameters
* -m MQA1 -h 127.0.0.1 -p 1414 -c TEST.CHL -q TEST.Q1 -u UserID -x Password
*
* #author Roger Lacroix
*/
public class MQClearQueue01
{
private static final SimpleDateFormat LOGGER_TIMESTAMP = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
private Hashtable<String,String> params;
private Hashtable<String,Object> mqht;
public MQClearQueue01()
{
super();
params = new Hashtable<String,String>();
mqht = new Hashtable<String,Object>();
}
/**
* Make sure the required parameters are present.
* #return true/false
*/
private boolean allParamsPresent()
{
boolean b = params.containsKey("-h") && params.containsKey("-p") &&
params.containsKey("-c") && params.containsKey("-m") &&
params.containsKey("-q") &&
params.containsKey("-u") && params.containsKey("-x");
if (b)
{
try
{
Integer.parseInt((String) params.get("-p"));
}
catch (NumberFormatException e)
{
b = false;
}
}
return b;
}
/**
* Extract the command-line parameters and initialize the MQ HashTable.
* #param args
* #throws IllegalArgumentException
*/
private void init(String[] args) throws IllegalArgumentException
{
int port = 1414;
if (args.length > 0 && (args.length % 2) == 0)
{
for (int i = 0; i < args.length; i += 2)
{
params.put(args[i], args[i + 1]);
}
}
else
{
throw new IllegalArgumentException();
}
if (allParamsPresent())
{
try
{
port = Integer.parseInt((String) params.get("-p"));
}
catch (NumberFormatException e)
{
port = 1414;
}
mqht.put(CMQC.CHANNEL_PROPERTY, params.get("-c"));
mqht.put(CMQC.HOST_NAME_PROPERTY, params.get("-h"));
mqht.put(CMQC.PORT_PROPERTY, new Integer(port));
mqht.put(CMQC.USER_ID_PROPERTY, params.get("-u"));
mqht.put(CMQC.PASSWORD_PROPERTY, params.get("-x"));
// I don't want to see MQ exceptions at the console.
MQException.log = null;
}
else
{
throw new IllegalArgumentException();
}
}
/**
* Handle connecting to the queue manager, issuing PCF command then
* looping through PCF response messages and disconnecting from
* the queue manager.
*/
private void doPCF()
{
MQQueueManager qMgr = null;
PCFMessageAgent agent = null;
PCFMessage request = null;
PCFMessage[] responses = null;
String qMgrName = (String) params.get("-m");
String queueName = (String) params.get("-q");
try
{
qMgr = new MQQueueManager(qMgrName, mqht);
MQClearQueue01.logger("successfully connected to "+ qMgrName);
agent = new PCFMessageAgent(qMgr);
MQClearQueue01.logger("successfully created agent");
// https://www.ibm.com/support/knowledgecenter/SSFKSJ_latest/com.ibm.mq.ref.adm.doc/q087420_.html
request = new PCFMessage(CMQCFC.MQCMD_CLEAR_Q);
request.addParameter(CMQC.MQCA_Q_NAME, queueName);
responses = agent.send(request);
MQClearQueue01.logger("responses.length="+responses.length);
for (int i = 0; i < responses.length; i++)
{
if ((responses[i]).getCompCode() == CMQC.MQCC_OK)
MQClearQueue01.logger("Successfully cleared queue '"+queueName+"' of messages.");
else
MQClearQueue01.logger("Error: Failed to clear queue '"+queueName+"' of messages.");
}
}
catch (MQException e)
{
MQClearQueue01.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode);
}
catch (IOException e)
{
MQClearQueue01.logger("IOException:" +e.getLocalizedMessage());
}
catch (MQDataException e)
{
MQClearQueue01.logger("MQDataException:" +e.getLocalizedMessage());
}
finally
{
try
{
if (agent != null)
{
agent.disconnect();
MQClearQueue01.logger("disconnected from agent");
}
}
catch (MQDataException e)
{
MQClearQueue01.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode);
}
try
{
if (qMgr != null)
{
qMgr.disconnect();
MQClearQueue01.logger("disconnected from "+ qMgrName);
}
}
catch (MQException e)
{
MQClearQueue01.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode);
}
}
}
/**
* A simple logger method
* #param data
*/
public static void logger(String data)
{
String className = Thread.currentThread().getStackTrace()[2].getClassName();
// Remove the package info.
if ( (className != null) && (className.lastIndexOf('.') != -1) )
className = className.substring(className.lastIndexOf('.')+1);
System.out.println(LOGGER_TIMESTAMP.format(new Date())+" "+className+": "+Thread.currentThread().getStackTrace()[2].getMethodName()+": "+data);
}
public static void main(String[] args)
{
MQClearQueue01 mqcq = new MQClearQueue01();
try
{
mqcq.init(args);
mqcq.doPCF();
}
catch (IllegalArgumentException e)
{
MQClearQueue01.logger("Usage: java MQClearQueue01 -m QueueManagerName -h host -p port -c channel -q QueueName -u UserID -x Password");
System.exit(1);
}
System.exit(0);
}
}

I need to browse through IBM MQ and get a particular type of message and then remove the message from Queue

when using a solution provided in the below post : Browse, read, and remove a message from a queue using IBM MQ classes
i am getting below error
Caused by: com.ibm.mq.jmqi.JmqiException: CC=2;RC=2495;AMQ8568: The native JNI library 'mqjbnd64' was not found. For a client installation this is expected. [3=mqjbnd64]
I am new to IBM MQ , but i think it is trying to fing MQ library on my local machine but i want to connect on remote server as MQ is not installed on my machine
Here is a Java/MQ program that will connect to either (1) a remote queue manager (client mode) or (2) local queue manager (bindings mode), open queue, loop to retrieve all messages from a queue then close and disconnect.
import java.io.EOFException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import com.ibm.mq.MQException;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.CMQC;
/**
* Program Name
* MQTest12L
*
* Description
* This java class will connect to a remote queue manager with the
* MQ setting stored in a HashTable, loop to retrieve all messages from a queue
* then close and disconnect.
*
* Sample Command Line Parameters
* bindings mode
* -m MQA1 -q TEST.Q1
* client mode
* -m MQA1 -q TEST.Q1 -h 127.0.0.1 -p 1414 -c TEST.CHL -u UserID -x Password
*
* #author Roger Lacroix
*/
public class MQTest12L
{
private static final SimpleDateFormat LOGGER_TIMESTAMP = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
private Hashtable<String,String> params;
private Hashtable<String,Object> mqht;
/**
* The constructor
*/
public MQTest12L()
{
super();
params = new Hashtable<String,String>();
mqht = new Hashtable<String,Object>();
}
/**
* Make sure the required parameters are present.
* #return true/false
*/
private boolean allParamsPresent()
{
boolean b = params.containsKey("-m") && params.containsKey("-q");
if (params.containsKey("-c"))
{
b = b && params.containsKey("-c") && params.containsKey("-h") && params.containsKey("-p");
}
if (b)
{
try
{
if (params.containsKey("-p"))
Integer.parseInt((String) params.get("-p"));
}
catch (NumberFormatException e)
{
b = false;
}
}
return b;
}
/**
* Extract the command-line parameters and initialize the MQ HashTable.
* #param args
* #throws IllegalArgumentException
*/
private void init(String[] args) throws IllegalArgumentException
{
int port = 1414;
if (args.length > 0 && (args.length % 2) == 0)
{
for (int i = 0; i < args.length; i += 2)
{
params.put(args[i], args[i + 1]);
}
}
else
{
throw new IllegalArgumentException();
}
if (allParamsPresent())
{
if (params.containsKey("-c"))
{
try
{
port = Integer.parseInt((String) params.get("-p"));
}
catch (NumberFormatException e)
{
port = 1414;
}
mqht.put(CMQC.CHANNEL_PROPERTY, params.get("-c"));
mqht.put(CMQC.HOST_NAME_PROPERTY, params.get("-h"));
mqht.put(CMQC.PORT_PROPERTY, new Integer(port));
if (params.containsKey("-u"))
mqht.put(CMQC.USER_ID_PROPERTY, params.get("-u"));
if (params.containsKey("-x"))
mqht.put(CMQC.PASSWORD_PROPERTY, params.get("-x"));
}
// I don't want to see MQ exceptions at the console.
MQException.log = null;
}
else
{
throw new IllegalArgumentException();
}
}
/**
* Connect, open queue, loop and get all messages then close queue and disconnect.
*
*/
private void testReceive()
{
String qMgrName = (String) params.get("-m");
String inputQName = (String) params.get("-q");
MQQueueManager qMgr = null;
MQQueue queue = null;
int openOptions = CMQC.MQOO_INPUT_AS_Q_DEF + CMQC.MQOO_INQUIRE + CMQC.MQOO_FAIL_IF_QUIESCING;
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.options = CMQC.MQGMO_WAIT + CMQC.MQGMO_FAIL_IF_QUIESCING;
gmo.waitInterval = 5000; // wait up to 5 seconds for a message.
MQMessage receiveMsg = null;
int msgCount = 0;
boolean getMore = true;
try
{
if (params.containsKey("-c"))
qMgr = new MQQueueManager(qMgrName, mqht);
else
qMgr = new MQQueueManager(qMgrName);
MQTest12L.logger("successfully connected to "+ qMgrName);
queue = qMgr.accessQueue(inputQName, openOptions);
MQTest12L.logger("successfully opened "+ inputQName);
while(getMore)
{
receiveMsg = new MQMessage();
try
{
// get the message on the queue
queue.get(receiveMsg, gmo);
msgCount++;
if (CMQC.MQFMT_STRING.equals(receiveMsg.format))
{
String msgStr = receiveMsg.readStringOfByteLength(receiveMsg.getMessageLength());
// MQTest12L.logger("["+msgCount+"] " + msgStr);
}
else
{
byte[] b = new byte[receiveMsg.getMessageLength()];
receiveMsg.readFully(b);
// MQTest12L.logger("["+msgCount+"] " + new String(b));
}
}
catch (MQException e)
{
if ( (e.completionCode == CMQC.MQCC_FAILED) &&
(e.reasonCode == CMQC.MQRC_NO_MSG_AVAILABLE) )
{
// All messages read.
getMore = false;
break;
}
else
{
MQTest12L.logger("MQException: " + e.getLocalizedMessage());
MQTest12L.logger("CC=" + e.completionCode + " : RC=" + e.reasonCode);
getMore = false;
break;
}
}
catch (IOException e)
{
MQTest12L.logger("IOException:" +e.getLocalizedMessage());
}
}
}
catch (MQException e)
{
MQTest12L.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode);
}
finally
{
MQTest12L.logger("read " + msgCount + " messages");
try
{
if (queue != null)
{
queue.close();
MQTest12L.logger("closed: "+ inputQName);
}
}
catch (MQException e)
{
MQTest12L.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode);
}
try
{
if (qMgr != null)
{
qMgr.disconnect();
MQTest12L.logger("disconnected from "+ qMgrName);
}
}
catch (MQException e)
{
MQTest12L.logger("CC=" +e.completionCode + " : RC=" + e.reasonCode);
}
}
}
/**
* A simple logger method
* #param data
*/
public static void logger(String data)
{
String className = Thread.currentThread().getStackTrace()[2].getClassName();
// Remove the package info.
if ( (className != null) && (className.lastIndexOf('.') != -1) )
className = className.substring(className.lastIndexOf('.')+1);
System.out.println(LOGGER_TIMESTAMP.format(new Date())+" "+className+": "+Thread.currentThread().getStackTrace()[2].getMethodName()+": "+data);
}
/**
* main line
* #param args
*/
public static void main(String[] args)
{
MQTest12L write = new MQTest12L();
try
{
write.init(args);
write.testReceive();
}
catch (IllegalArgumentException e)
{
System.err.println("Usage: java MQTest12L -m QueueManagerName -q QueueName [-h host -p port -c channel] [-u UserID] [-x Password]");
System.exit(1);
}
System.exit(0);
}
}

Execute a sqoop command using processbuilder in java

I have a working sqoop command which imports data from oracle to hive.
When i am trying to run the sqoop command using the process builder API it is giving
java.io.IOException: error=2, No such file or directory
The same command works without any issues when executing it from the shell. Please let me know how to resolve this.
I also tried splitting the command into array of strings as well as list of strings. But, the two approaches did not work.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class SqoopTest {
static ProcessBuilder processBuilder = null;
static Process spawnProcess = null;
static int exitValue;
static int pid;
static List<String> commands;
public static void main(String[] args) {
runSqoop();
}
/*
* Executes the shell script with a series of sqoop commands.
*
*
*
* sqoop import -D mapred.child.java.opts='\-Djava.security.egd=file:/dev/../dev/urandom'
* --connect 'jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=ON)(FAILOVER=ON)(ADDRESS=(PROTOCOL=TCP)(HOST=173.37)(PORT=1541))(ADDRESS=(PROTOCOL=TCP)(HOST=173.37)(PORT=1541)))(CONNECT_DATA=(SERVICE_NAME=CAFI2PRD)(SERVER=DEDICATED)))'
* --username XXCSS_O --password "c******3" --split-by LINE_ITEM_ID -m 10
* --query "select LINE_ITEM_ID,LIST_PRICE,SERVICE_VALUE from XXCSS_KTN_REQ_LINE_DETAIL where \$CONDITIONS AND lid_date > to_date('2016-10-13 03:04:18','MM/dd/yyyy hh24:mi:ss') and lid_date <= to_date('2016-10-13 03:04:18','MM/dd/yyyy hh24:mi:ss')"
* --map-column-hive LINE_ITEM_ID=BIGINT,LIST_PRICE=BIGINT,SERVICE_VALUE=BIGINT
* --null-string '\\\\N' --null-non-string '\\\\N' --hcatalog-home
* /opt/mapr/hive/hive-1.2/hcatalog --hcatalog-table XXCSS_KTN_REQ_LINE_DETAIL --hcatalog-database frameworks_dataingestion
*
*/
public static void runSqoop() {
commands = new ArrayList<String>();
commands.add("sqoop import -D mapred.child.java.opts=\'\\-Djava.security.egd=file:/dev/../dev/urandom\' --connect \'jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=ON)(FAILOVER=ON)(ADDRESS=(PROTOCOL=TCP)(HOST=173.37)(PORT=1541))(ADDRESS=(PROTOCOL=TCP)(HOST=173.37)(PORT=1541)))(CONNECT_DATA=(SERVICE_NAME=CAFI2PRD)(SERVER=DEDICATED)))\' --username XXCSS_O --password \"c*****3\" --split-by LINE_ITEM_ID -m 10 --query \"select LINE_ITEM_ID,LIST_PRICE,SERVICE_VALUE from XXCSS_KTN_REQ_LINE_DETAIL where \\$CONDITIONS AND lid_date > to_date(\'2016-10-13 00:00:00\',\'yyyy-MM-dd hh24:mi:ss\') and lid_date <= to_date(\'2016-10-13 03:04:18\',\'yyyy-MM-dd hh24:mi:ss\')\" --map-column-hive LINE_ITEM_ID=BIGINT,LIST_PRICE=BIGINT,SERVICE_VALUE=BIGINT --null-string '\\\\N' --null-non-string '\\\\N' --hcatalog-home /opt/mapr/hive/hive-1.2/hcatalog --hcatalog-table XXCSS_KTN_REQ_LINE_DETAIL --hcatalog-database frameworks_dataingestion");
processBuilder = new ProcessBuilder(commands);
try {
System.out.println("Executing " + commands.toString());
spawnProcess = processBuilder.start();
BufferedReader reader =
new BufferedReader(new InputStreamReader(spawnProcess.getErrorStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ( (line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
String result = builder.toString();
System.out.println(result);
try {
exitValue = spawnProcess.waitFor();
pid = getPID(spawnProcess);
System.out.println("The PID is " + pid);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("Process exited with the status :" + exitValue);
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* Retrieves the process id of the executed command.
*
* #param process the executed process
*
* #return PID
*/
public static int getPID(Process process) {
try {
Class<?> processImplClass = process.getClass();
Field fpid = processImplClass.getDeclaredField("pid");
if (!fpid.isAccessible()) {
fpid.setAccessible(true);
}
return fpid.getInt(process);
} catch (Exception e) {
System.out.println(e.getMessage());
return -1;
}
}
/*
* Retrieves the current java process id
*
* #return java process ID
*/
// #SuppressWarnings("restriction")
// public static int getCurrentJavaPID() {
// int jvmpid = 0;
// java.lang.management.RuntimeMXBean runtime = java.lang.management.ManagementFactory.getRuntimeMXBean();
// try {
// java.lang.reflect.Field jvm = runtime.getClass().getDeclaredField("jvm");
// jvm.setAccessible(true);
// sun.management.VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime);
// java.lang.reflect.Method pid_method = mgmt.getClass().getDeclaredMethod("getProcessId");
// pid_method.setAccessible(true);
//
// jvmpid = (Integer) pid_method.invoke(mgmt);
// } catch (Exception e) {
// e.getMessage();
// }
// return jvmpid;
// }
}
Note: for security concerns i have replaced the ip addresses and passwords with dummy variables.
Tried passing the command as a string array. But, it doesnt recognize the JDBC Connection string
public static void runSqoop() {
String[] commands = {"sqoop","import","-D mapred.child.java.opts=\'\\-Djava.security.egd=file:/dev/../dev/urandom\'","--connect", "\'jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=ON)(FAILOVER=ON)(ADDRESS=(PROTOCOL=TCP)(HOST=173.37)(PORT=1541))(ADDRESS=(PROTOCOL=TCP)(HOST=173.370)(PORT=1541)))(CONNECT_DATA=(SERVICE_NAME=CAFI2PRD)(SERVER=DEDICATED)))\'","--username","XXCSS_O","--password","\"c***3\"", "--split-by","LINE_ITEM_ID","-m","10","--query","\"select LINE_ITEM_ID,LIST_PRICE,SERVICE_VALUE from XXCSS_KTN_REQ_LINE_DETAIL where \\$CONDITIONS AND lid_date > to_date(\'2016-10-13 00:00:00\',\'yyyy-MM-dd hh24:mi:ss\') and lid_date <= to_date(\'2016-10-13 03:04:18\',\'yyyy-MM-dd hh24:mi:ss\')\"","--null-string","'\\\\N'","--null-non-string","'\\\\N'","--hcatalog-home","/opt/mapr/hive/hive-1.2/hcatalog","--hcatalog-table","XXCSS_KTN_REQ_LINE_DETAIL","--hcatalog-database","frameworks_dataingestion"};
processBuilder = new ProcessBuilder(commands);
try {
System.out.println("Executing " + commands.toString());
spawnProcess = processBuilder.start();
BufferedReader reader =
new BufferedReader(new InputStreamReader(spawnProcess.getErrorStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ( (line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
String result = builder.toString();
System.out.println(result);
try {
exitValue = spawnProcess.waitFor();
pid = getPID(spawnProcess);
System.out.println("The PID is " + pid);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.println(e.getMessage());
}
System.out.println("Process exited with the status :" + exitValue);
} catch (Exception e) {
e.printStackTrace();
}
}
Stack trace:
cat: /opt/mapr/zookeeper/zookeeperversion: No such file or directory
16/10/25 07:41:12 INFO sqoop.Sqoop: Running Sqoop version:
1.4.6-mapr-1609 16/10/25 07:41:12 WARN tool.BaseSqoopTool: Setting your password on the command-line is insecure. Consider using -P
instead. 16/10/25 07:41:13 ERROR tool.BaseSqoopTool: Got error
creating database manager: java.io.IOException: No manager for connect
string:
'jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=ON)(FAILOVER=ON)(ADDRESS=(PROTOCOL=TCP)(HOST=173.37)(PORT=1541))(ADDRESS=(PROTOCOL=TCP)(HOST=173.37)(PORT=1541)))(CONNECT_DATA=(SERVICE_NAME=CAFI2PRD)(SERVER=DEDICATED)))'
at org.apache.sqoop.ConnFactory.getManager(ConnFactory.java:191)
at org.apache.sqoop.tool.BaseSqoopTool.init(BaseSqoopTool.java:256)
at org.apache.sqoop.tool.ImportTool.init(ImportTool.java:89)
at org.apache.sqoop.tool.ImportTool.run(ImportTool.java:593)
at org.apache.sqoop.Sqoop.run(Sqoop.java:143)
at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:70)
at org.apache.sqoop.Sqoop.runSqoop(Sqoop.java:179)
at org.apache.sqoop.Sqoop.runTool(Sqoop.java:218)
at org.apache.sqoop.Sqoop.runTool(Sqoop.java:227)
at org.apache.sqoop.Sqoop.main(Sqoop.java:236)
You have to provide commands and args separately as like below
Process p = new ProcessBuilder("myCommand", "myArg").start();
OR
ProcessBuilder pb =
new ProcessBuilder("myCommand", "myArg1", "myArg2");
pb.start();
Here is a example

Running a Client-Server Chat program

This is one of the most common application scenario that can be found all over the net. and I'm not asking any questions about the java codes that I did because I was successful in running it on my laptop where both the client and server part of the .java file resides. Rather I have had problem getting it to work in between two computers. I tried establishing physical connection using cross-over cable to connect two computers, and did a test to see if file transfers successfully and it did, however, keeping one Server part of the .java file in one computer and client part in the other, I tried to run the server first and then the client but it got a "access denied" error.
For reference here's my two .java files:
/* ChatClient.java */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class ChatClient {
private static int port = 5000; /* port to connect to */
private static String host = "localhost"; /* host to connect to (server's IP)*/
private static BufferedReader stdIn;
private static String nick;
/**
* Read in a nickname from stdin and attempt to authenticate with the
* server by sending a NICK command to #out. If the response from #in
* is not equal to "OK" go bacl and read a nickname again
*/
private static String getNick(BufferedReader in,
PrintWriter out) throws IOException {
System.out.print("Enter your nick: ");
String msg = stdIn.readLine();
out.println("NICK " + msg);
String serverResponse = in.readLine();
if ("SERVER: OK".equals(serverResponse)) return msg;
System.out.println(serverResponse);
return getNick(in, out);
}
public static void main (String[] args) throws IOException {
Socket server = null;
try {
server = new Socket(host, port);
} catch (UnknownHostException e) {
System.err.println(e);
System.exit(1);
}
stdIn = new BufferedReader(new InputStreamReader(System.in));
/* obtain an output stream to the server... */
PrintWriter out = new PrintWriter(server.getOutputStream(), true);
/* ... and an input stream */
BufferedReader in = new BufferedReader(new InputStreamReader(
server.getInputStream()));
nick = getNick(in, out);
/* create a thread to asyncronously read messages from the server */
ServerConn sc = new ServerConn(server);
Thread t = new Thread(sc);
t.start();
String msg;
/* loop reading messages from stdin and sending them to the server */
while ((msg = stdIn.readLine()) != null) {
out.println(msg);
}
}
}
class ServerConn implements Runnable {
private BufferedReader in = null;
public ServerConn(Socket server) throws IOException {
/* obtain an input stream from the server */
in = new BufferedReader(new InputStreamReader(
server.getInputStream()));
}
public void run() {
String msg;
try {
/* loop reading messages from the server and show them
* on stdout */
while ((msg = in.readLine()) != null) {
System.out.println(msg);
}
} catch (IOException e) {
System.err.println(e);
}
}
}
and here's the ChatServer.java:
/* ChatServer.java */
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Hashtable;
public class ChatServer {
private static int port = 5000; /* port to listen on */
public static void main (String[] args) throws IOException
{
ServerSocket server = null;
try {
server = new ServerSocket(port); /* start listening on the port */
} catch (IOException e) {
System.err.println("Could not listen on port: " + port);
System.err.println(e);
System.exit(1);
}
Socket client = null;
while(true) {
try {
client = server.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.err.println(e);
System.exit(1);
}
/* start a new thread to handle this client */
Thread t = new Thread(new ClientConn(client));
t.start();
}
}
}
class ChatServerProtocol {
private String nick;
private ClientConn conn;
/* a hash table from user nicks to the corresponding connections */
private static Hashtable<String, ClientConn> nicks =
new Hashtable<String, ClientConn>();
private static final String msg_OK = "OK";
private static final String msg_NICK_IN_USE = "NICK IN USE";
private static final String msg_SPECIFY_NICK = "SPECIFY NICK";
private static final String msg_INVALID = "INVALID COMMAND";
private static final String msg_SEND_FAILED = "FAILED TO SEND";
/**
* Adds a nick to the hash table
* returns false if the nick is already in the table, true otherwise
*/
private static boolean add_nick(String nick, ClientConn c) {
if (nicks.containsKey(nick)) {
return false;
} else {
nicks.put(nick, c);
return true;
}
}
public ChatServerProtocol(ClientConn c) {
nick = null;
conn = c;
}
private void log(String msg) {
System.err.println(msg);
}
public boolean isAuthenticated() {
return ! (nick == null);
}
/**
* Implements the authentication protocol.
* This consists of checking that the message starts with the NICK command
* and that the nick following it is not already in use.
* returns:
* msg_OK if authenticated
* msg_NICK_IN_USE if the specified nick is already in use
* msg_SPECIFY_NICK if the message does not start with the NICK command
*/
private String authenticate(String msg) {
if(msg.startsWith("NICK")) {
String tryNick = msg.substring(5);
if(add_nick(tryNick, this.conn)) {
log("Nick " + tryNick + " joined.");
this.nick = tryNick;
return msg_OK;
} else {
return msg_NICK_IN_USE;
}
} else {
return msg_SPECIFY_NICK;
}
}
/**
* Send a message to another user.
* #recepient contains the recepient's nick
* #msg contains the message to send
* return true if the nick is registered in the hash, false otherwise
*/
private boolean sendMsg(String recipient, String msg) {
if (nicks.containsKey(recipient)) {
ClientConn c = nicks.get(recipient);
c.sendMsg(nick + ": " + msg);
return true;
} else {
return false;
}
}
/**
* Process a message coming from the client
*/
public String process(String msg) {
if (!isAuthenticated())
return authenticate(msg);
String[] msg_parts = msg.split(" ", 3);
String msg_type = msg_parts[0];
if(msg_type.equals("MSG")) {
if(msg_parts.length < 3) return msg_INVALID;
if(sendMsg(msg_parts[1], msg_parts[2])) return msg_OK;
else return msg_SEND_FAILED;
} else {
return msg_INVALID;
}
}
}
class ClientConn implements Runnable {
private Socket client;
private BufferedReader in = null;
private PrintWriter out = null;
ClientConn(Socket client) {
this.client = client;
try {
/* obtain an input stream to this client ... */
in = new BufferedReader(new InputStreamReader(
client.getInputStream()));
/* ... and an output stream to the same client */
out = new PrintWriter(client.getOutputStream(), true);
} catch (IOException e) {
System.err.println(e);
return;
}
}
public void run() {
String msg, response;
ChatServerProtocol protocol = new ChatServerProtocol(this);
try {
/* loop reading lines from the client which are processed
* according to our protocol and the resulting response is
* sent back to the client */
while ((msg = in.readLine()) != null) {
response = protocol.process(msg);
out.println("SERVER: " + response);
}
} catch (IOException e) {
System.err.println(e);
}
}
public void sendMsg(String msg) {
out.println(msg);
}
}
Now, what should I do in order to run this two files from two computers given that I have the physical connection(TCP/IP) setup already??
Thanks in advance... :)
Sounds like it's quite possibly a firewall problem. Have you tried opening a hole in your firewall for port 1001?
Have you also looked at your java.policy and make sure that it is configured to allow local codebase to open sockets?
as mentioned in comment, you should not use port < 1025 for you applications, since they are always used in deamon processes. However you should test your program like this
1) if you get connection refused then you should check the exception properly, whether client program takes time before generating exception ( that mean request is going to server and then it's giving connection refused), in that case you should try java.policy put following in a file named java.policy
grant {
permission java.net.SocketPermission ":1024-65535",
"connect,accept";
permission java.net.SocketPermission ":80", "connect";
permission java.io.FilePermission "", "read,write,delete";
permission java.security.SecurityPermission "";
};
while compiling use this flag -Djava.security.policy=java.policy
more-over you should also try -Djava.rmi.server.hostname=IP, where IP is clien-ip for client.java and server-ip for server.java
2) if you are immediately getting exception at client side then your request is not going outside your pc, so client has some problem.
check the exception properly and post them over here.
3) though i've not got access denied error, but it seems to have port problem that might be solved using policy or port>1024.
post what are you getting now.

Categories

Resources