I have a Processor class which implements Runnable.
The first method
Public Processor implements Runnable{
//"Event" is the name of this queue
PersistentQueue<Event> persistentQueue = new PersistentQueue<>("Event");
//add the Event POJO to the persistentQueue
public void addToQueue(Event, event) {
persistentQueue.add(event);
}
The persistentQueue is to store Event POJO
And the run method
public void run() {
while (true) {
if (!persistentQueue.isEmpty()) {
Event peekEvent = persistantQueue.peek();
sendRequest(peekEvent);
}
}
}
public void sendRequest(Event, event){
// do something to send the event
}
For the first addToQueue method I wrote the test
public class ProcessorTest {
private Event event;
private Processor m_Processor;
#Before
public void setup() throws IOException {
//the Processor class is a singleton
m_Processor = Processor.getProcessor();
event = new Event();
}
#Test
public void test(){
PersistentQueue<Event> persistentQueue = new PersistentQueue<>
("Event");
m_Processor.addToQueue(event);
assertEquals(1, persistentQueue.size());
}
}
But the queue size is 0 not 1. I dont know what's the problem. And I also have question about how to test the run method?
In your test method, you created a new queue that has nothing to do with your m_Processor instance; it goes unused entirely. You need to change your code so you can get the PersistentQueue instance contained inside your m_Processor instance. Assuming you have a getter method called getPersistentQueue inside Processor, then you can use the following:
#Test
public void test() {
m_Processor.addToQueue(event);
assertEquals(1, m_Processor.getPersistentQueue().size());
}
Related
I have this (simplified) service class:
public interface EventListener {
void call();
}
public class MyService {
private final EventListener eventListener;
private final List<String> elements = new LinkedList<>();
public MyService(EventListener eventListener) {
this.eventListener = eventListener;
}
public void addElement(String element) {
elements.add(element);
eventListener.call();
}
public void removeElement(String element) {
elements.remove(element);
eventListener.call();
}
}
And this test method:
#Test
public void remove_should_call_event_listener() {
// arrange
EventListener eventListener = Mockito.mock(EventListener.class);
MyService myService = new MyService(eventListener);
myService.addElement("dummy");
// act
myService.removeElement("dummy");
// assert
Mockito.verify(eventListener).call();
}
How can I tell Mockito to ignore calls of eventListener.call() during "arrange" and verify only calls during "act"? I want to verify that eventListener.call() was called during myService.removeElement(...) and ignore all other calls.
Just before acting, call:
Mockito.reset(eventListener); // resets the set-up also
or
Mockito.clearInvocations(eventListener) // resets only the invocation history
In class B how can i know jobs of threads are finished? In after properties some worker are running. In class B, I need to know if worker are done?
public class A implements InitializingBean{
public void method1(){
...
}
#Override
public void afterPropertiesSet() throws Exception {
System.out.print("test after properties set");
// send threads to executorService
ExecutorService executorService = Executors
.newFixedThreadPool(4);
for (int i = 0; i < 4; i++) {
Worker worker = new Worker();
executorService.submit(worker);
}
}
}
public class Worker implements Callable<Void>{
#Override
public void call(){
...
}
}
public class B{
public void methodB(){
A a = new A();
a.method1();
///Here How can i know the job of the workers are finished?
}
}
Use a listener/callback pattern to have the thread report completion to a listener. This simple example should show the process:
public interface ThreadCompleteListener {
void workComplete();
}
public class NotifyingThread extends Thread {
private Set<ThreadCompleteListener> listeners;
// setter method(s) for adding/removing listeners to go here
#Override
public void run() {
// do stuff
notifyListeners();
}
private void notifyListeners() {
for (ThreadCompleteListener listener : listeners) {
listener.workComplete(); // notify the listening class
}
}
}
in your listening class:
NotifyingThread t = new NotifyingThread();
t.addListener(new ThreadCompleteListener() {
void workComplete() {
// do something
}
});
t.start();
You could use a Future implementation for your thread. It provides a Future#isDone()
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html#isDone()
In general, it is usually more useful to be notified via a callback when jobs complete. However, since others have posted answers which follow that model, I'll instead post a solution that simply allows you to poll and ask whether the jobs are finished, in case this is what fits the needs of your application better.
public static interface InitializingBean{
public void afterPropertiesSet() throws Exception;
}
public static class A implements InitializingBean{
private List<Future<Void>> submittedJobs = Collections.synchronizedList(new ArrayList<Future<Void>>());
public void method1(){
//do stuff
}
#Override
public void afterPropertiesSet() throws Exception {
System.out.print("test after properties set");
// send threads to executorService
ExecutorService executorService = Executors
.newFixedThreadPool(4);
synchronized (submittedJobs) {
for (int i = 0; i < 4; i++) {
Worker worker = new Worker();
submittedJobs.add(executorService.submit(worker));
}
}
}
/**
* Allows you to poll whether all jobs are finished or not.
* #return
*/
public boolean areAllJobsFinished(){
synchronized (submittedJobs) {
for(Future<Void> task : submittedJobs){
if(!task.isDone()){
return false;
}
}
return true;
}
}
}
public static class Worker implements Callable<Void>{
#Override
public Void call(){
//do worker job
return null; //to satisfy compiler that we're returning something.
}
}
public static class B{
public void methodB(){
A a = new A();
a.method1();
if(a.areAllJobsFinished()){
System.out.println("Congrats, everything is done!");
} else {
System.out.println("There's still some work being done :-(");
}
}
}
If you'd like to wait in that thread that starts the ExecutorService, you can actually use the awaitTermination method.
At the end of you afterPropertiesSet method, you should add:
executorService.shutdown();
After this you then add:
executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS)
This causes the thread to wait for all the executorService's tasks to be done and then continues. So place any code you want to execute after the call to awaitTermination.
I've got a problem as follows: I've got the next class:
Public class foo{
Thread runningThread = null;
...
public static void start() {
runningThread = new RunningThreadImpl();
runningThread.start();
runningThread.join();
}
public static void stop() {
this.runningThread.stop();
}
}
And:
Public class runningThreadImpl implements Runnable {
...
public void run() {
while (shouldRun()){
sleep(... A long long time);
}
}
public void stop() {
shouldRun = false;
}
I've got a class using foo, And now i want it to stop. The above code defined inside a given jar, Which means i cannot edit it. But i thought about using Reflection in order to interrupt the Thread and "Wake" him up so i won't have to wait.
So far i've gotten this far:
Field field = foo.getInstance().getClass().getDeclaredField("runningThread");
field.setAccessible(true);
But i don't now what to do now. How can i use the Field in order to interrupt a thread? Is it even possible?
You could create a new class called FooHelper in the same package as Foo like so:
package same.package.as.foo;
public class FooHelper {
private final Foo foo;
public FooHelper(Foo foo) {
this.foo = foo;
}
public void stop() {
this.foo.stop();
this.foo.runningThread.interrupt();
}
}
Construct a FooHelper passing it the instance of Foo, and call the FooHelper's stop() method.
I am new to Java and pulling some hair out on the application I'm working on. Here is what I am trying to do.
I have a client / server setup where I use QuickServer as the server. I got that up and running well. It sends and receives commands line by line with "\r"\n" so I have my client in a
public class CommandHandler extends Thread {
public void run() {
while (connected) {
//read and write
if (command.equals("USERNAME")) {
AsynchEventBusSingleton.$().postEvent(new TellParentToGiveUN())
}
...
}
}
My GUI freezes if I do not extend Thread so I presume the CommandHandler needs its own thread. The problem however is the AsyncEventBusSingleton.
public class AsyncEventBusSingleton {
private EventBus ebEventBus = null;
final static AsyncEventBusSingleton aebInstance = new AsyncEventBusSingleton();
public synchronized static AsyncEventBusSingleton $() {
return aebInstance;
}
private AsyncEventBusSingleton() {
ebEventBus = new AsyncEventBus(Executors.newCachedThreadPool());
}
public void registerSubscriber(Object subscriber) {
ebEventBus.register(subscriber);
}
public void unRegisterSubscriber(Object subscriber) {
ebEventBus.unregister(subscriber);
}
public synchronized void postEvent(Object e) {
ebEventBus.post(e);
}
}
When I call this class inside CommandHandler thread none of my other classes on the main thread receive the events. If I remove the "synchronized" keyword some of the events are received/repeated/an error is generated.
Everything seems to work fine if don't throw events from CommandHandler, but rather pass a reference of the class that instantiates the CommandHandler and have it call CommandHandler's methods:
public class ServerConnectionHandler {
private CommandHandler commandhandler;
public void ConnectToServer() {
commandhandler = new CommandHandler(this);
}
public void dispatchServerEvent() {
commandhandler.sendResponse("MyUN");
}
}
public class CommandHandler extends Thread {
private PrintWriter printWriter;
private ServerConnectionHandler serverconnectionhandler;
public sendResponse(String s) {
this.printWriter.println(s);
}
public void run() {
while (connected) {
//read and write
if (command.equals("USERNAME")) {
this.serverconnectionhandler.dispatchServerEvent();
}
...
}
}
What is the best practice for having the client listen for certain commands and dispatching events to respond?
I'm trying to figure out how to listen to a property change in another class. Below is my code:
ClassWithProperty has the property I want to listen to:
public class ClassWithProperty {
private PropertyChangeSupport changes = new PropertyChangeSupport(this);
private int usersOnline;
public int getUsersOnline() {
return usersOnline;
}
public ClassWithProperty() {
usersOnline = 0;
while (usersOnline<10) {
changes.firePropertyChange("usersOnline", usersOnline, usersOnline++);
}
}
public void addPropertyChangeListener(
PropertyChangeListener l) {
changes.addPropertyChangeListener(l);
}
public void removePropertyChangeListener(
PropertyChangeListener l) {
changes.removePropertyChangeListener(l);
}
}
Main is where i need to know about the property change:
public class Main {
private static ClassWithProperty test;
public static void main(String[] args) {
test = new ClassWithProperty();
test.addPropertyChangeListener(listen());
}
private static PropertyChangeListener listen() {
System.out.println(test.getUsersOnline());
return null;
}
}
I have the event fired only the last time (usersOnline=10).
I'm new to Java and tried to find a solution, but to no avail.
The code:
private static PropertyChangeListener listen() {
System.out.println(test.getUsersOnline());
return null;
}
returns null which means "no object", which in turn means that test.addPropertyChangeListener(listen()) is effectively test.addPropertyChangeListener(null), which won't register anything.
You must pass a valid instance of a PropertyChangeListener to the addPropertyChangeListener() method.
Edit
I suggest you read the Java tutorial's chapter about PropertyChangeListeners:
http://download.oracle.com/javase/tutorial/uiswing/events/propertychangelistener.html
Another problem of your code is that you call firePropertyChange() in the constructor of ClassWithProperty. But at that time, no listener can possibly be registered, so it does not have any effect. Any call to addPropertyChangeListener() happens after you have fired the events.
Here is your code modified so that it should work (haven't tested it though...):
public class ClassWithProperty {
private PropertyChangeSupport changes = new PropertyChangeSupport(this);
private int usersOnline = 0;
public ClassWithProperty() {
}
public void setupOnlineUsers()
{
while (usersOnline < 10) {
changes.firePropertyChange("usersOnline", usersOnline, ++usersOnline);
}
}
public int getUsersOnline() {
return usersOnline;
}
public void addPropertyChangeListener(PropertyChangeListener l) {
changes.addPropertyChangeListener(l);
}
public void removePropertyChangeListener(PropertyChangeListener l) {
changes.removePropertyChangeListener(l);
}
}
public class MainListener implements PropertyChangeListener {
private ClassWithProperty test;
public MainListener() {
test = new ClassWithProperty();
test.addPropertyChangeListener(this);
test.setupOnlineUsers();
}
public void propertyChange(PropertyChangeEvent evt) {
System.out.println(test.getUsersOnline());
}
public static void main(String[] args) {
new MainListener(); // do everything in the constructor
}
}
What I do is put a method in the ClassWithProperty class:
public PropertyChangeSupport getPropertyChangeSupport() {
return changes;
}
Then, register for property change events in the constructor of your Main() class:
private void initializeListeners() {
test.getPropertyChangeSupport().addPropertyChangeListener((PropertyChangeEvent event) -> {
if (event.getPropertyName().equals("usersOnline")) {
String passedEventData = (String) event.getNewData();
}
});
}
This make it so you are not repeating the code in your ClassWithProperty with methods that are already in the PropertyChangeSupport class.
when you need to fire an event in your ClassWithProperty class, do:
changes.firePropertyChange("usersOnline", oldValue, newValue);
One notable feature of this method is that, if the
event.getOldValue() and the event.getNewValue()
are equal, the event will not fire. If you want to fire repeated events with the same information, use null in the oldValue field;
The firePropertyChange() method only passes int, boolean and Object. So if you are not passing an int or boolean, you need to cast the value that was passed in the event on the receiving end.
Your method here:
public ClassWithProperty() {
usersOnline = 0;
while (usersOnline<10) {
changes.firePropertyChange("usersOnline", usersOnline, usersOnline++);
usersOnline++;
}
}
has a while loop that will continuously loop and block the thread. My limited knowledge of property change listeners is that they listen for changes to a bound property, here the usersOnLine variable, meaning the property change should only fire if this number changes (likely within in any setUserOnLine, addUserOnLine, removeUserOnLine and similar methods). For more on bound properties, please look here: Bound Properties