I'm using a bolt that receives tuples from another bolt (exclamation bolt ) and writes it on a file , the problem I got is that I have duplicated results four times , like when I emit a word , I found the word Written four times . where's the problem possibly could be ?
public class PrinterBolty extends BaseBasicBolt {
#Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
try {
BufferedWriter output;
output = new BufferedWriter(new FileWriter("/root/src/storm-starter/hh.txt", true));
output.newLine();
output.append(tuple.getString(0));
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void declareOutputFields(OutputFieldsDeclarer ofd) {
}
}
The solution was to specify 1 spout in the main class :
builder.setSpout("spout", new RandomSentenceSpout(), 1);
Related
I have PVPStats objects stored in PlayerMeta.java:
public static Map <UUID, PVPstats> sPVPStats = new HashMap<>();
I know for sure the map is getting populated with objects that contain the expected vars for each uuid.
I'm trying to write these objects (converted to single lines of strings) into plugins/core/killstats.txt when the server calls onDisable() in Main.java
Along with the Map object, in PlayerMeta.java are also the methods to update and retrieve PVPStats objects from the Map. Those are all working.
The part that is not is working is the write method:
public static void writePVPStats() throws IOException {
BufferedWriter w = new BufferedWriter(new FileWriter("plugins/core/killstats.txt"));
sPVPStats.keySet().forEach(user -> {
try {
System.out.println(sPVPStats);
// stdout = {a6b6e3a1-a1ec-4fee-9d6d-f5e495c3e9d7=a6b6e3a1-a1ec-4fee-9d6d-f5e495c3e9d7:1:0}
w.write(user.toString() + "\n");
w.flush();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
w.close();
}
kill.txt after onDisable() is done:
a6b6e3a1-a1ec-4fee-9d6d-f5e495c3e9d7
Instead it needs to be:
{a6b6e3a1-a1ec-4fee-9d6d-f5e495c3e9d7=a6b6e3a1-a1ec-4fee-9d6d-f5e495c3e9d7:1:0}
For reference, here is the complete PVPStats class.
Lastly, in case it matters / helps, the reader on server launch:
Files.readAllLines(killstats_user_database.toPath()).forEach(line -> {
PVPstats stats = PVPstats.fromString(line);
PlayerMeta.sPVPStats.put(stats.playerid, stats);
});
Source Code:
backend.FileManager.java
backend.PlayerMeta.java
backend.PVPstats.java
events.PVP.java
EDIT
I just tried this with killstats.txt file type nad killstats.txt doesnt have anything in it now.
public static void writePVPStats() throws IOException {
BufferedWriter w = new BufferedWriter(new FileWriter("plugins/core/killstats.txt"));
for (PVPstats object: sPVPStats.values()) {
try {
System.out.println(sPVPStats);
w.write(object.toString() + "\n");
w.flush();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
w.close();
}
Ok so there were multiple issues. I wasn't controlling the flushing of the buffer, I declared the hasmap incorrectly, I wasn't accessing the value part of the hash map, and I wasn't correctly enforcing plain text.
SOLUTION
public static Map <UUID, PVPstats> sPVPStats = new HashMap<UUID, PVPstats>();
public static void writePVPStats() throws IOException {
BufferedWriter w = new BufferedWriter(new FileWriter("plugins/core/killstats.txt"));
for (PVPstats object: sPVPStats.values()) {
try {
System.out.println(sPVPStats);
System.out.println(object.toString());
w.write(object.toString() + "\n");
w.flush();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
w.close();
}
I have implemented the classic producer/consumer problem, using a shared buffer class.
For some reason I am not getting the correct sequence of outputs.
I have tried so many times, but could not spot the flaw, can someone please point out the mistake?
public class BufferProdCons {
boolean produced = false;
int data;
public synchronized void produceData(int d) {
while (produced) {
try {
wait();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
// System.out.println("data produced");
data = d;
produced = true;
this.notify();
}
public synchronized int consumeData() {
while (!produced) {
try {
wait();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
// System.out.println("data consumed");
int d =data;
produced = false;
this.notify();
return d;
}
}
public class Consumer extends Thread {
BufferProdCons buffer ;
Consumer(BufferProdCons buf){
buffer = buf;
}
public void run(){
for (int i =0;i<10;i++){
int d=buffer.consumeData();
System.out.println("Consumed data "+d);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Producer extends Thread {
BufferProdCons buffer;
Producer(BufferProdCons buf){
buffer = buf;
}
#Override
public void run(){
for(int i=0;i<10;i++){
System.out.println("Produced "+i);
buffer.produceData(i);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class MainProdCons {
public static void main(String[] args) {
BufferProdCons buffer = new BufferProdCons();
Thread t1 = new Producer(buffer);
Thread t2 = new Consumer(buffer);
t1.start();t2.start();
}
}
sample Output
Produced 0
Consumed data 0
Produced 1
Consumed data 1
Produced 2
Produced 3
Consumed data 2
Produced 4
Consumed data 3
Produced 5
Consumed data 4
Produced 6
Consumed data 5
Produced 7
Consumed data 6
Produced 8
Consumed data 7
Produced 9
Consumed data 8
Consumed data 9
My understanding is that , the sequence of producer and consumer lines should be alternate, But as you can see sometimes they appear in succession.
I have a requirement in my web application,i.e, to download data as csv from db, without creating a physical file in the server.So i've decided to write to outputstreame directly.
So what is the maximum bytes of data can i write to OutputStream.
my code is something like below.
StreamingOutput stream = new StreamingOutput() {
#Override
public void write(OutputStream os) throws IOException, WebApplicationException {
try {
-----------some code------
while(rs.next){
os.write(rs.getString(0).getBytes()+","+rs.getString(1).getBytes());
}
os.close();
} catch (Exception e) {
throw new WebApplicationException(e);
}
}
};
import java.util.*;
import java.io.*;
public class BigFile{
public static void main(String asd[]){
String fileString = "D:/INSAn/error.log";
try {
BigFile bigFile = new BigFile(fileString);
for (String line : bigFile)
System.out.println("line : " + line);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("exception");
}
}
}
Hey you can use the above code , it will read file of any size.I tested it for 1 GB file.
I want to get a file contains a letter from other files in random order. I must to do it with Threads. And I don't know why I have in output file content from 1 file, after it content from 2 file and after that content from 3 file. I have Main:
public static void main(final String[] args) throws Exception {
for(int i=1;i<args.length; i++) {
new Thread1( args[i], args[0]).start();
}
}
And class Thread1:
public class Thread1 extends Thread {
String path;
FileWriter fw;
private String desc;
public Thread1( String path, String desc) {
super();
this.desc=desc;
this.path=path;
}
#Override
public void run() {
FileReader f = null;
try {
f = new FileReader(path);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int c;
try {
fw = new FileWriter(desc, true);
while((c = f.read()) != -1) {
fw.write(c);
}
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Please, explain me, why it don't work in way, what I think it should be work.
Please, explain me, why it don't work in way, what I think it should be work.
Your problem is that all of your threads are appending to the same file but using different FileWriter instances. This sounds like it would work but they are all overwriting each other. When you open a file for appending it opens it and positions the write marker at the end of the file. When two threads do this, they both will be at the same marker. If thread #1 writes a character then thread #2 will write a character and overwrite the first.
You could use a single FileWriter and share it with each of your threads. Then you synchronize on it for mutex purposes and do the write.
public Thread1( String path, String desc, FileWriter fw) {
this.fw = fw;
...
}
...
// when you write to it, you will need to synchronize on the writer
sychronized (fw) {
fw.write(c);
}
// don't close it in the threads but close it later after you have joined with
// the threads
Another option is to share a PrintStream which is already synchronized internally.
I need to run two processes simultaneously.
I wrote the code:
public void starttwoprocessing () {
final Thread tworunprocessing = new Thread(new Runnable() {
public void run() {
FlashLight.onFlashResume();
handler.post(new Runnable() {
public void run() {
camera.takePicture(null, null, photoCallback);
}
});
}
});
tworunprocessing.start();
}
First start:
camera.takePicture(null, null, photoCallback);
The second:
FlashLight.onFlashResume();
After changing places with the same result.
In this case, I get the first shot and the flash is started later.
Thread.sleep(...); does not help
How to start simultaneously flash, and immediately take a picture?
Thanks
written like this:
public class Launcher
{
public void main(String args[]) throws IOException, InterruptedException
{
try {
Process[] proc = new Process[2];
proc[0] = new ProcessBuilder("FlashPreview.onFlashResumeStart()").start();
Thread.sleep(3000);
proc[1] = new ProcessBuilder("camera.takePicture(null, null, photoCallback)").start();
try {
Thread.sleep(3000);
}
catch (InterruptedException ex)
{
}
proc[0].destroy();
Thread.sleep(3000);
proc[1].destroy();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
Called:
mk = new Launcher();
try {
mk.main(null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Something I'm doing wrong.
Does not work at all, no crash, but wrote in the log:07-05 16:38:58.217: W/System.err(30934): java.io.IOException: Error running exec(). Command: [FlashPreview.onFlashResumeStart()] Working Directory: null Environment: [ANDROID_SOCKET_zygote=9, SECONDARY_STORAGE=/storage/extSdCard:/storage/UsbDriveA:/storage/UsbDriveB:/storage/UsbDriveC:/storage/UsbDriveD:/storage/UsbDriveE:/storage/UsbDriveF, ANDROID_BOOTLOGO=1, EXTERNAL_STORAGE=/storage/sdcard0, ANDROID_ASSETS=/system/app, PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin, ASEC_MOUNTPOINT=/mnt/asec, LOOP_MOUNTPOINT=/mnt/obb, BOOTCLASSPATH=/system/framework/core.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/framework2.jar:/system/framework/framework_ext.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar:/system/framework/sec_edm.jar:/system/framework/seccamera.jar, ANDROID_DATA=/data, LD_LIBRARY_PATH=/vendor/lib:/system/lib, ANDROID_ROOT=/system, ANDROID_PROPERTY_WORKSPACE=8,66560, VIBE_PIPE_PATH=/dev/pipes]
even using Threads your processes will runs after eche other. Using Threads means that second process no need to wait while first one is done. But easiest way how to fire two processes at the same time it is use timeout or ProcessBuilder
Also it can be good idea to run second process in first one. As for me it the best solution.
P.S. privet, ne chasto yvidiw zdes svoih s ykrainu)))
I have implemented the following:
class MyTask extends AsyncTask<Void, Void, Integer> {
#Override
protected void onPreExecute() {
super.onPreExecute();
FlashLight.onFlashResume();
}
#Override
protected Integer doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
camera.takePicture(null, null, photoCallback);
}
}