I am trying to makeAConnection with the server using this class. This class gets the list of parameters needed to perform operation on Images in HashMaps. Then in doInBackground , I perform the operations required on Image one by one. The code for one of the classes which is OVFImage Deployer is also pasted below
public class ImageDeployer extends SwingWorker<Boolean,String> {
public ImageDeployer(){
}
public ImageDeployer(HashMap<String, String> volIDMap, HashMap<String, String> osMap) {
// TODO Auto-generated constructor stub
this.volIDMap = volIDMap;
this.osMap = osMap;
System.out.println(volIDMap);
System.out.println(osMap);
makeAConnection();
try {
doInBackground();
System.out.println("Do In Background");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void makeAConnection(){
inputFile = RESTEngine.getFilePath();
Properties defaultProps = new Properties();
try {
fin = new FileInputStream(inputFile);
defaultProps.load(fin);
fin.close();
}
catch(FileNotFoundException e1){
System.out.println("The properties file supposed to contain Authorization parameters was not found.");
e1.printStackTrace();
System.exit(-1);
}
catch(IOException e1){
System.out.println("An exception occured while trying to open the properties file");
e1.printStackTrace();
System.exit(-1);
}
// assign variables from Input file with default value as null
user = defaultProps.getProperty("UserID", null);
host = defaultProps.getProperty("PowerVC_IP_ADDRESS", null);
password = defaultProps.getProperty("UserPass" ,null );
jsch = new JSch();
try {
session = jsch.getSession(user, host, 22);
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel=session.openChannel("exec");
channel.setInputStream(null);
try {
in = channel.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Connection Successful");
} catch (JSchException e) {
// TODO Auto-generated catch block
System.out.println("Unable to connect");
e.printStackTrace();
}
}
#Override
protected Boolean doInBackground() throws Exception {
ImageDeployer imageDeployer = new ImageDeployer();
imageDeployer.makeAConnection();
for(String imageName : volIDMap.keySet()){
String volID = volIDMap.get(imageName);
String oS = osMap.get(imageName);
if (oS.equalsIgnoreCase("aix")){
imageDeployer = new OVFImageDeployer(volID, oS, imageName);
}
// Other Cases depending upon the OS Type
}
return null;
}
}
The code for OVFImage Deployer
public class OVFImageDeployer extends PowerVCImageDeployer {
public OVFImageDeployer(String VolID,String oS,String imageName){
String command="/usr/bin/powervc-devtools/powervc-devcli glance image-create json "+imageName+" "+oS+" "+VolID;
try {
((ChannelExec)channel).setCommand(command);
channel.connect();
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Now when I run the code I get a NullPointerException on line ((ChannelExec)channel).setCommand(command).
I know if I put makeAConnection after the try block in OVFImageDeployer the code would work, but then I don't want to make a connection again and again . I want a connection to be initialized just once and all operations to be performed using that connection only.
You should remove the call to doInBackground from inside the constructor of ImageDeployer:
public ImageDeployer(HashMap<String, String> volIDMap, HashMap<String, String> osMap) {
....
makeAConnection();
//doInBackground();
...
}
This will initialize the channel when you create an instance of ImageDeployer. And you can add channel to the list of constructor arguments of OVFImageDeployer:
public OVFImageDeployer(String VolID,String oS,String imageName, Channel channel){
this.channel = channel;
...
}
This will create an instance of OVFImageDeployer with the channel that is present in the ImageDeployer instance. You need to remove these two statements from inside the doInBackground method and pass channel along with the other parameters while constructing an instance of OVFImageDeployer:
#Override
protected Boolean doInBackground() throws Exception {
//ImageDeployer imageDeployer = new ImageDeployer();
//imageDeployer.makeAConnection();
...
ImageDeployer imageDeployer = new OVFImageDeployer(volID, oS, imageName, channel);
...
}
Now the client code can create an instance of ImageDeployer and can execute doInBackground on it:
ImageDeployer imageDeployer = new ImageDeployer();
imageDeployer.doInBackground();
With this, every time you create an instance of OVFImageDeployer inside the doInBackground method, you can use the same channel which was created by the makeAConnection method while constructing the ImageDeployer instance.
Related
In my current code i had a servlet from which if i create post to the servlet it will open a new websocket client , that mean 10 client connection each running for same purpose but with different api and secret , so i need to close particular session
I am using Jetty :: Websocket :: Client v9.4.48.v20220622
Please suggest , as i can get the session details but unable to use because it's not working with String data type . only in Session session it is working and i am unable to store session details anywhere else , as only in String data type i can save .
Whereas a is my API and b is my Secret Key ;
PS : Websocket connection is working fine to send expected data
class connector {
String a;
String b;
public void start() {
WebSocketClient client = new WebSocketClient();
MyWebSocket socket = new MyWebSocket();
try {
client.start();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
URI destUri = null;
try {
destUri = new URI("wss://socket.delta.exchange");
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ClientUpgradeRequest request = new ClientUpgradeRequest();
System.out.println("Connecting to: " + destUri);
try {
client.connect(socket, destUri, request);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
socket.awaitClose(3600, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
client.stop();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#WebSocket
public class MyWebSocket {
private final CountDownLatch closeLatch = new CountDownLatch(1);
#OnWebSocketConnect
public void onConnect(Session session) throws IOException {
session.getRemoteAddress();
System.out.println("Connection opened");
PingPong newObj = new PingPong();
newObj.session = session;
Authorization authMe = new Authorization();
Identifier getSt = new Identifier();
newObj.enableHeartBeat();
System.out.println(session);
session.getRemote().sendString(authMe.data(a, b));
}
#OnWebSocketMessage
public void onMessage(String message) {
MessageHandler objmsg = new MessageHandler();
objmsg.check();
System.out.println(
"Current Thread ID: "
+ Thread.currentThread().getId());
System.out.println("Message from Server: -- " + message);
}
#OnWebSocketClose
public void onClose(int statusCode, String reason) {
System.out.println("WebSocket Closed. Code:" + statusCode);
}
public boolean awaitClose(int duration, TimeUnit unit)
throws InterruptedException {
return this.closeLatch.await(duration, unit);
}
}
}
I want to do session.close() for a particular session details which i got from
session.getRemoteAddress().toString();
Session session ;
String sessionDetailSaved ;
i want to search for sessionDetailSaved and compare with all the on running sessions and close it
Or else any other way i can close particular session with different method may be interrupting session thread but sure it will not completely close connection .
Maven Dependency i am using
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-client</artifactId>
<version>9.4.48.v20220622</version>
</dependency>
Calling Session.close() will initiate a close handshake where the remote endpoint should reply with a response close frame, and once the close response has been received the WebSocket connection will be closed. You can send custom close status code and reason with Session.close(int statusCode, String reason).
You also have the option to call Session.disconnect() which will do a hard close of the underlying connection without sending this close frame.
In regards to your code, it looks like you are never completing the closeLatch in the OnWebSocketClose method, so your awaitClose method will always timeout.
Also, if possible you should try to re-use the same WebSocketClient instance for multiple connections because it is a heavy weight object. It is expensive to create a new one for each request.
I am trying to fetch the data and store it in database.
Created a Get mapping for invoking the data from url and storing it in database using service class .
#GetMapping("/")
public String root(Model model) throws IOException {
model.addAttribute("test1","Hello user");
service.populate();
return "mainTemplate";
}
my populate method in service class add data to database.
public void populate() throws IOException{
URL url = new URL("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/01-01-2021.csv");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
int res= connection.getResponseCode();
CSVReader reader=null;
if(res==200) {
log.info("Connected to github");
try {
BufferedReader readurl = new BufferedReader(new InputStreamReader(connection.getInputStream()),8192);
reader=new CSVReader(readurl);
String[] line;
int i=0;
while((line=reader.readNext())!=null) {
if(i==0) {
i++;
continue;
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
Corona corona = new Corona();
corona.setLastupDate(LocalDateTime.parse(line[4],formatter));
corona.setConfirmed(Long.valueOf(line[7]));
corona.setRecovered(Long.valueOf(line[9]));
corona.setActive(Long.valueOf(line[10]));
corona.setCombinedKey(line[11]);
log.info(corona.toString());
repo.save(corona);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (CsvValidationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
if(reader!=null) {
try {
reader.close();
} catch (IOException e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
}
else {
log.info("URL is wrong");
}
}
Everything is working fine when i hit the resource url ,but i have to wait for some time to show my webpage , until all data does not get stored in database.
I want to show "Data is being added" in mainTemplate.html as soon as i hit the url. So that my populate method runs in background and i don't have to wait for completion of method to show my mainTemplate .
I tired to add #Async method annotation but that does not seem to be worked .
I'm having trouble mocking a static method in a third-party library. I keep receiving a null-pointer exception when running the test, but I'm not sure why that is.
Here is the class and the void method that invokes the static method I'm trying to mock "MRClientFactory.createConsumer(props)":
public class Dmaap {
Properties props = new Properties();
public Dmaap() {
}
public MRConsumerResponse createDmaapConsumer() {
System.out.println("at least made it here");
MRConsumerResponse mrConsumerResponse = null;
try {
MRConsumer mrConsumer = MRClientFactory.createConsumer(props);
System.out.println("made it here.");
mrConsumerResponse = mrConsumer.fetchWithReturnConsumerResponse();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return mrConsumerResponse;
}
}
Below is the test that keeps returning a null-pointer exception. The specific line where the null-pointer is being generated is: MRClientFactory.createConsumer(Mockito.any(Properties.class));
#RunWith(PowerMockRunner.class)
#PrepareForTest(fullyQualifiedNames = "com.vismark.PowerMock.*")
public class DmaapTest {
#Test
public void testCreateDmaapConsumer() {
try {
Properties props = new Properties();
PowerMockito.mockStatic(MRClientFactory.class);
PowerMockito.doNothing().when(MRClientFactory.class);
MRClientFactory.createConsumer(Mockito.any(Properties.class));
//MRClientFactory.createConsumer(props);
Dmaap serverMatchCtrl = new Dmaap();
Dmaap serverMatchCtrlSpy = spy(serverMatchCtrl);
serverMatchCtrlSpy.createDmaapConsumer();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Please follow this example carefully: https://github.com/powermock/powermock/wiki/MockStatic
Especially you are missing a
#PrepareForTest(Dmaap.class)
…to denote the class which does the static call.
I have an executor service that runs new threads based upon reflection. I have one method that when run does not exit the thread and will hang program execution. I am unsure why this is, can anyone point me to what I'm missing?
Also, if any of the concurrency experts out there notice any problems I may run into, please let me know, I am rather green in concurrency....
Notes:
The connectToFTP method will be refactored to return ChannelSftp in the future.
downloadFromFTP returns false at the end as the method is not completed. I feel that this is the method that is causing the thread to hang. I just don't know why.
The goal of the method is to list each file within an SFTP directory.
from NetworkingShopCa.
#Override
public Object connectToFTP(String username, String password, String host, String port, FtpTypes ftpTypes) {
switch(ftpTypes){
case FTP:
LOGGER.error("Plain FTP is not implemented yet (if ever)");
break;
case FTPS:
FTPSClient client = new FTPSClient();
client.setTrustManager(TrustManagerUtils.getAcceptAllTrustManager());
try {
client.connect(host);
client.enterLocalPassiveMode();
client.login(username, password);
} catch (IOException e) {
LOGGER.error(e.toString());
}
return client;
case SFTP:
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession(username, host);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
return sftpChannel;
} catch (JSchException e) {
// TODO Auto-generated catch block
LOGGER.error(e.toString());
}
break;
default:
LOGGER.error("Invalid FtpType");
break;
}
return false;
}
#Override
public boolean downloadFromFTP(String directory, String filename, boolean downloadAll,Object activeConnection) {
if(activeConnection instanceof ChannelSftp){
ChannelSftp sftpChannel = (ChannelSftp) activeConnection;
try {
sftpChannel.cd(directory);
//List our files within the directory
Vector vv = sftpChannel.ls(directory);
if (vv != null) {
for (int ii = 0; ii < vv.size(); ii++) {
Object obj = vv.elementAt(ii);
if (obj instanceof ChannelSftp.LsEntry) {
LOGGER.debug("[" + ((LsEntry) obj).getFilename() + "]");
}
}
}
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
From main
runtimes.put(config.getInt("ESE_PRIORITY"),"RUN_ESE");
ExecutorService threadPool = Executors.newFixedThreadPool(totalRunnables);
LOGGER.info("Executing runtimes in order of priority.");
for(final int priority : runtimes.keySet()){
if(!threadPool.isShutdown() && !threadPool.isTerminated()){
//run the method denoted by the property
final java.lang.reflect.Method method = m.getClass().getMethod(runtimes.get(priority));
Future<?> f = threadPool.submit(new Runnable() {
#Override
public void run() {
try {
method.invoke(m);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
}
});
f.get();
}
}
public void RUN_ESE(){
LOGGER.info("Running ESE");
Networking networking = new NetworkingShopCa();
networking.downloadFromFTP("/toclient/order/processed", "", true, networking.connectToFTP("user", "password", "host", "", FtpTypes.SFTP));
}
--Edit--
Following a debugger downloadFromFTP executes fully, returns out of the method and goes to close the runnable when it hangs:
Line 1153 of ThreadPoolExecutor.java shows:
afterExecute(task, thrown);
Any ideas? For what its worth my build environment is:
Ubuntu 13.04 64 bit
OpenJDK 7 (ubuntu default) with attached sources.
Eclipse kepler
I have the following code structure.
A transaction handler of type Transaction which is a field in a Client Handler class, which talks to a Server. (the client handler and the server are collocated), the client talks to the client handler via serialized object messages.
When a new transaction request comes in from the client, (comes on thread using the readObject() method of an object input stream), I then do a series of trx_handler.setFoo(trx.getFoo))). This works fine, I can handle the first request. But when a subsequent request comes in (which only starts getting executed after the first request finished due to the loop structure, I find that the trx handler has been reinitialised to its default values, the object is still there, but all the values inside are the defaut ones. What can cause this problem?
My first guess would be garbage collection, but in my Client Handler class, there is always a pointer to this trx_handler.
The code below illustrates what happens. A statement would first be of type start, so the trx_handler will be correctly initialised. handle_statement will then be called. Subsequent statements should then be received, but at this point the trx_handler has been reinitialised to its default settings, so the access_set field is null, the session id as well, and none of the modification made to the object in hande_statement are visible
Thanks
public class Handler {
private Statement trx_handler;
/* Constructor initialises trx_handler to new Statement(); */
public ClientHandler(final Socket socket, long uid, Server server, ObjectInputStream ois) throws IOException, Exception {
LOGGER.info("Constructing Handler");
this.uid = uid;
this.server = server;
this.socket = socket;
this.database = server.getDB();
this.trx_sys = database.getTransactionManager();
create_listening(socket, ois);
out = socket.getOutputStream();
oos = new ObjectOutputStream(out);
this.trx_handler = new Statement(false);
}
private void create_incoming(final Socket socket, final ObjectInputStream stream) {
Thread incoming = new Thread() {
#Override
public void run() {
ObjectInputStream ois = stream;
InputStream in = null;
while (true) {
Object statement = null;
try {
statement = ois.readObject();
execute_stat(statement, socket, null);
LOGGER.info("Ready to execute next ");
} catch (SocketException e) {
LOGGER.severe("Connection Closed");
return;
} catch (IOException e) {
LOGGER.severe("Connection Closed");
return;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
String error_message = e.getMessage();
send_error(socket, error_message);
}
}
}
};
incoming.setDaemon(true);
incoming.start();
}
private synchronized void execute_stat(Statement trx) {
if (trx.getTransactionState() == Consts.trx_end) {
trx_sys.commitTransaction(trx_handler);
return;
} else if (trx.getTransactionState() == Consts.trx_start) {
try {
trx_handler.setAccessSet(trx.getAccessSet());
trx_handler.setSession_id(trx.getSession_id());
trx_sys.startTransaction(trx_handler);
handle_statement(socket, trx_handler);
/* TEST HERE THAT FIELDS IN TRX_HANDLER ARE CORRECTLY SET (INCLUDING SOME MODIFIED IN
handle_statement and they are correctly set */
return;
} catch (Exception ex) {
Logger.getLogger(ClientHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
try {
LOGGER.info("Execute Trx: stat");
/* Can't see modifications made in the start case */
Statement stats = trx.getStatement();
trx_handler.setStatement(stats);
handle_statement(stats, socket, trx_handler);
} catch (Exception e) {
e.printStackTrace();
}
return;
}
You need to either send a brand new object for each transaction, use ObjectOutputStream.writeUnshared(), or else call ObjectOutputStream.reset() between sends.