Wait and notify for sending bunch of messages - java

Would be super grateful if someone can explain me how wait/notify/notifyAll works and if is there better solution for the problem I am facing. Basically, we have to send a bunch of SMS messages. For sending messages an object called SMPPSession is used but in this example I'll just use superficial code. SMPPSession is supposed to send messages to SMSC server and to reestablish session in situations when connection breaks. I would like to use multiple threads for sending multiple messages, and to have a separate single thread, some sort of "guardian"/ "watcher"/"notifier". The role of that separate thread is to stop all other threads from executing their code, while it works on reestablishing session. Naturally, SMPPSession is shared among all these threads. Once that guardian finishes reconnecting, all other thread needs to continue with using the session and proceed with sending.
Now, I have some code and getting exception. Any help?
In reality we do send real SMS messages using jsmpp library and inside it there is SMPPSession object.
public class SMPPSession {
private boolean bind;
private static final Random idGenerator = new Random();
public int sendMessage(String msg){
try{
Thread.sleep(1000L);
System.out.println("Sending message: " + msg);
return Math.abs(idGenerator.nextInt());
} catch (InterruptedException e){
e.printStackTrace();
}
return -1;
}
public void reBind(){
try{
System.out.println("Rebinding...");
Thread.sleep(1000L);
this.bind = true;
System.out.println("Session established!");
} catch (InterruptedException e){
e.printStackTrace();
}
}
public boolean isBind(){
return this.bind;
}
}
public class Sender extends Thread{
private SMPPSession smppSession;
public Sender(String name, SMPPSession smppSession){
this.setName(name);
this.smppSession = smppSession;
}
#Override
public void run(){
while (!Client.messages.isEmpty()){
synchronized (Client.messages){
if (smppSession.isBind()){
final String msg = Client.messages.remove(0);
final int msgId = smppSession.sendMessage(msg);
System.out.println(Thread.currentThread().getName() + " sent msg and received msgId: " + msgId);
Client.messages.notifyAll();
} else {
try {
Client.messages.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
}
}
public class SessionProducer extends Thread{
private SMPPSession smppSession;
public SessionProducer(String name, SMPPSession smppSession){
this.setName(name);
this.smppSession = smppSession;
}
#Override
public void run(){
while (!Client.messages.isEmpty()){
synchronized (Client.messages){
if (!smppSession.isBind()){
smppSession.reBind();
System.out.println(Thread.currentThread().getName() + " managed to reestablish SMPP session.");
Client.messages.notifyAll();
} else{
try {
Client.messages.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
}
}
public class Client {
public static final List<String> messages = new CopyOnWriteArrayList<>();
public static void main(String[] args) {
//populate messages from db
messages.add("msg1"); messages.add("msg2"); messages.add("msg3"); messages.add("msg4"); messages.add("msg5"); messages.add("msg6");
SMPPSession smppSession = new SMPPSession();
SessionProducer sessionProducer = new SessionProducer("SessionProducer1", smppSession);
Sender sender1 = new Sender("Sender1", smppSession);
Sender sender2 = new Sender("Sender2", smppSession);
Sender sender3 = new Sender("Sender3", smppSession);
Sender sender4 = new Sender("Sender4", smppSession);
sessionProducer.start();
sender1.start();
sender2.start();
sender3.start();
sender4.start();
}
}
Naturally, I get exception and have no idea why. Somehow threads are not in sync.
Rebinding...
Session established!
SessionProducer1 managed to reestablish SMPP session.
Sending message: msg1
Sender4 sent msg and received msgId: 432995458
Sending message: msg2
Sender4 sent msg and received msgId: 113629699
Sending message: msg3
Sender4 sent msg and received msgId: 611735717
Sending message: msg4
Sender4 sent msg and received msgId: 1234995659
Sending message: msg5
Sender4 sent msg and received msgId: 922228968
Sending message: msg6
Sender4 sent msg and received msgId: 2097204472
Exception in thread "Sender2" Exception in thread "Sender1" Exception in thread "Sender3" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/java.util.concurrent.CopyOnWriteArrayList.elementAt(CopyOnWriteArrayList.java:385)
at java.base/java.util.concurrent.CopyOnWriteArrayList.remove(CopyOnWriteArrayList.java:478)
at demo.Sender.run(Sender.java:20)
java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/java.util.concurrent.CopyOnWriteArrayList.elementAt(CopyOnWriteArrayList.java:385)
at java.base/java.util.concurrent.CopyOnWriteArrayList.remove(CopyOnWriteArrayList.java:478)
at demo.Sender.run(Sender.java:20)
java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/java.util.concurrent.CopyOnWriteArrayList.elementAt(CopyOnWriteArrayList.java:385)
at java.base/java.util.concurrent.CopyOnWriteArrayList.remove(CopyOnWriteArrayList.java:478)
at demo.Sender.run(Sender.java:20)

Your loops call Client.messages.isEmpty() with no synchronization. I haven't spent the time to really understand what your code does—can't see all of it anyway—but I can guess what's happening.
Maybe the list contains one message.
Four threads all see it as not empty.
Four threads all try enter the synchronized(Client.messages) block.
One-by-one, they get in to the block, see that smppSession.isBind() is true, and try to remove a message from the list.
The first thread to remove a message succeeds, and then each of the other four throws an exception because it tried to remove from an empty list.

Recommend a SMS development library sms-client from China to support Smpp.
<dependency>
<groupId>com.chinamobile.cmos</groupId>
<artifactId>sms-client</artifactId>
<version>0.0.7</version>
</dependency>
public void testsmpp() throws Exception {
SmsClientBuilder builder = new SmsClientBuilder();
SmsClient smsClient = builder.uri("smpp://127.0.0.1:18890?username=test01&password=1qaz2wsx&version=52&window=32&maxchannel=1")
.receiver(new MessageReceiver() {
public void receive(BaseMessage message) {
logger.info("receive : {}",message.toString());
}
}).build();
for (int i = 0; i < 5; i++) {
SubmitSm pdu = new SubmitSm();
pdu.setRegisteredDelivery((byte)1);
pdu.setSourceAddress(new Address((byte)0,(byte)0,"10086"));
pdu.setDestAddress(new Address((byte)0,(byte)0,"13800138000"));
pdu.setSmsMsg(new SmsTextMessage("SmsTextMessage " + i,SmsDcs.getGeneralDataCodingDcs(SmsAlphabet.GSM,SmsMsgClass.CLASS_UNKNOWN)));
try {
smsClient.send(pdu, 1000);
} catch (Exception e) {
logger.info("send ", e);
}
}
Thread.sleep(5000000);
}

Related

NullPointerException for PrintWriter thats initialized in the run method of a thread

im making a networked game that has a server which creates a clientHandler thread every time a client joins. I want to ask the first client that joined if it wants to start the game every time a new client joins, giving it the current number of players connected. Writting through the clientHandlers printwritter gives a nullPointerException, even though ive started the thread before doing this. what could be the problem?
Here is the server code:
`public class Server implements Runnable{
private ArrayList<ClientHandler> handlers = new ArrayList<>();
private ArrayList<Player> players = new ArrayList<>();
private Game game;
private boolean start;
public static void main(String[] args) {
Server server = new Server();
Thread s = new Thread(server);
s.start();
}
public void login(String name){
//todo
for (ClientHandler c : handlers){
if (c.getName().equals(name)){
alreadyTaken(name);//todo
}
else{
players.add(new HumanPlayer(name,c));//todo
}
}
}
public void setStart(){
start = true;
}
private void alreadyTaken(String name) {
}
public void setTurn(ServerHandler sh){
//todo
}
public void updateView(){
}
public String hello() {
return "Hello"; //?
}
public void place(String s){
}
#Override
public void run() {
ServerSocket serverSocket;
try {
serverSocket = new ServerSocket(1800);
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println("----Server----");
while (!serverSocket.isClosed()) {
try {
Socket socket = serverSocket.accept();
ClientHandler handler = new ClientHandler(socket,handlers,this);
handlers.add(handler);
Thread h = new Thread(handler);
h.start();
System.out.println("A new client has connected");
System.out.println(handlers.get(0));
handlers.get(0).out.println("START? "+ handlers.size());
if (start){
System.out.println("start request works");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
`
And here's the client handler code:
`public class ClientHandler implements Runnable{
private Socket socket;
private ArrayList<ClientHandler> handlers;
private Server server;
public PrintWriter out;
private BufferedReader in;
private String name;
public ClientHandler(Socket socket, ArrayList<ClientHandler> handlers, Server server){
this.socket = socket;
this.handlers = handlers;
this.server = server;
}
public void broadcastMessage(String msg){
System.out.println("Broadcasting");
for (ClientHandler s : this.handlers){
s.out.println("Player: " + msg);
}
}
public static String removePrefix(String s, String prefix)
{
if (s != null && s.startsWith(prefix)) {
return s.split(prefix, 2)[1];
}
return s;
}
public String getName(){
return name;
}
#Override
public void run() {
try {
out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
new Thread(() -> {
while(socket.isConnected()){
String msg;
try {
msg = in.readLine();
while(msg!=null){
switch (msg.split(" ")[0]){
case "LOGIN":
name = removePrefix(msg,"LOGIN ");
server.login(name);//todo
break;
case "HELLO":
server.hello();//todo
break;
case "PLACE":
server.place(removePrefix(msg,"PLACE "));
break;
case "QUIT":
//todo
break;
case "STOP":
//todo
break;
case "START":
server.setStart();
default:
broadcastMessage(msg);
break;
}
msg = in.readLine();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}).start();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}`
I tried making a method in the client handler class which does the same thing. The server would just call that instead of writting directing through the PrintWriter, but i got the same error.
Starting a thread does not mean it is guaranteed to actually finish executing the first statement in its run() method before start() returns. In fact,
Usually it won't - starting a thread takes some time, and start() returns as soon as it can.
A JVM that runs a few statements in the thread you just started before start() returns is 'correct' - that is fine. A JVM that doesn't is also fine. Generally you don't want threads, because nothing is predictable anymore. At the very least you want to keep 'inter-thread comms' down to a minimum. Anytime a single field is used from more than one thread, things get very tricky.
What you need is synchronized or other tools to insert predictability in this code.
First, fix a bug
Your ClientHandler's run() code starts another thread for no reason. Take all that out, your run() method in ClientHandler should set up out and in and then immediately do while (socket.isConnected())
Synchronizing
At the very basic level, make a locker object and use notify/wait:
private final Object lock = new Object();
#Override public void run() {
try {
synchronized (lock) {
out = ...;
in = ...;
lock.notifyAll();
}
while (socket.isConnected()) { ... }
out definitely cannot be public here, you can't refer to a stream from multiple threads and expect things to work out!
Just 'fixing' your code involves then using something like:
public OutputStream getOutputStream() {
synchronized (lock) {
while (out == null) {
lock.wait();
}
}
return out;
}
Which will ensure that any thread that wants the out will wait for the other thread to get far enough, but, really, this is just setting you up for another 20 threading problems down the line. Instead, you want one object responsibile for all communication (both outgoing and incoming), and a concurrency-capable queue (there are various collections in the java.util.concurrent package good for this). Then:
Any other threads that want to just send data dump their message in the queue.
You have either 1 thread doing all comms, or 2 (one doing incoming, and one doing outgoing), both dedicated. The outgoing one just loops forever, grabbing objects from the queue and sending them.
If a thread wants to send a message and wait for the response, you need to use .wait() or nicer API from e.g. java.util.concurrent, or, use callback hell - you pass a closure with the code to run once the result is received.

java- while(true) does not keep executing using JPA but works for JDBC

So I wrote a program where I have a email table and it contains columns such as id,user,recipient,content,subject,etc. I would be running this as a backend service for 24/7 to keep fetching records to keep sending emails.
It's something like this:
public void schedule(){
Thread service;
service = new Thread(new Runnable() {
public void run() {
try {
System.out.println("Email service is ready");
email();
} catch (Exception e) {
e.printStackTrace();
}
}
});
service.start();
}
public void email() throws Exception {
try{
while(true) {
readConfig();
String portNumber = (String) settingsMap.get("COBRA_PORT");
if (ejbCon.checkConnection(portNumber) == -1) {
TerminalLogger.printMsg("failed to init EJB Beans on cobra port : " + portNumber);
stop = true;
}
List<Emailqueue> emailList=ejbCon.getSettingsFacade().emailrecord();
for (Emailqueue obj : emailList) {
String emailStatus = "DONE";
String errormsg=sendEmail(obj.getRecipient(), obj.getSubject(), obj.getContent(),obj.getUlnodeid(),obj.getUser());
if (!errormsg.equals("")) {
emailStatus = "FAILED";
}
TerminalLogger.printMsg("Status : " + emailStatus);
}
}
}catch(Exception e){
e.printStackTrace();
TerminalLogger.printMsg("Exception: "+e.toString());
}
Thread.sleep(2000);
}
So in email method,it fetches record from the table where the status are 'pending' and get it's relevant values and pass them as parameters.So if there are two pending records,it will fetch them and for loop each to send to their respectively recipient. It works fine.
However,I kept the program running and I tried inserting new record to the table but it is not picking it up.There's no error too.I thought while(true) always keep picking up records from the table if there are pending email records.
Did I do any mistake?
Edit:
Apparently the while loop keeps running and picking newly inserted records when I implemented with JDBC but with JPA(in this case),it does not keep looping.
Edit 2:
Apparently, the while(true) works now and I have no idea why. Although,the program will throw an communication timeout error after a period of time,probably because there's no record. Would need to resolve that.
This is how the loop looks like now:
try{
while(true) {
readConfig();
String portNumber = (String) settingsMap.get("COBRA_PORT");
if (ejbCon.checkConnection(portNumber) == -1) {
TerminalLogger.printMsg("failed to init EJB Beans on cobra port : " + portNumber);
stop = true;
}
List<Emailqueue> emailList=ejbCon.getSettingsFacade().emailrecord();
for (Emailqueue obj : emailList) {
String emailStatus = "DONE";
String errormsg=sendEmail(obj.getRecipient(), obj.getSubject(), obj.getContent(),obj.getUlnodeid(),obj.getUser());
if (!errormsg.equals("")) {
emailStatus = "FAILED";
}
TerminalLogger.printMsg("Status : " + emailStatus);
}
Thread.sleep(2000);
}
I think its prefer use cron scheduler rather than using while(true) inside of thread. it will be more safe than while(true) itself.

List.Remove() not working when removing this thread

So I'm working on a server that handles a few commands and one small problem is trying to remove the list of active clients when a user decides to log out. Each client is handled in a thread and once the command is done this active client gets removed however its not removing.
Below is an example of removing an active client, this thread is
public class serverHandlerThread implements Runnable
{
private Socket socket;
//private BufferedWriter clientOut;
private ObjectOutputStream toClient;
private MainServer server;
private Users user;
//Constructor
serverHandlerThread(MainServer server, Socket socket)
{
this.server = server;
this.socket = socket;
}
private ObjectOutputStream getWriter()
{
return toClient;
}
private void deleteClient(serverHandlerThread obj)
{
synchronized (server.clients)
{
server.clients.remove(obj);
}
}
#Override
public void run ()
{
try
{
//Setup I/O
toClient = new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream fromClient = new ObjectInputStream(socket.getInputStream());
while(!socket.isClosed())
{
//If server has received a message
if(fromClient.available() > 0)
{
//Reads message and objects from client
String input = fromClient.readUTF();
Object obj = fromClient.readObject();
//logger(input);
switch (input)
{
//Logout the user
case ".logout":
//Set the user to being logged out and print the log
user = (Users) obj;
deleteClient(this);
for (int i = 0; i < server.usersList.size(); i++)
{
if (user.getUserName().equals(server.usersList.get(i).getUserName()))
{
server.usersList.get(i).setLoggedIn(false);
logger(user.getUserName() + " has logged out");
}
}
break;
//Push message received to other clients
default:
logger("Sending message to clients");
user = (Users) obj;
deleteClient(this);
logger("clients size is: " + String.valueOf(server.clients.size()));
for (serverHandlerThread thatClient : server.getClients())
{
ObjectOutputStream thatClientOut = thatClient.getWriter();
if (thatClientOut != null)
{
thatClientOut.writeUTF(user.getUserName() + ": " + input + "\r\n");
thatClientOut.flush();
}
}
break;
}
}
}
}
catch (IOException | ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
server is of type MainServer which contains the list of clients and is written as List<ServerHandlerThread> clients. MainServer calls serverHandlerThread when a new client is accepted ie. making the server multithread.
The problem is when the clients requests to logout it should delete the user from the active client list. It doesn't and so when the server tries to push messages to all clients it also tried to write a message to the client who's socket has been closed(user who logged out) and so the server spits out a broken pipe error. Any ideas?
*Edit
More information on the mainServer class, omitted a few things but this should be enough information
public class MainServer
{
//Static variables
private static final int portNumber = 4444;
//Variables
private int serverPort;
private List<serverHandlerThread> clients;
/**
* Very basic logger that prints out
* the current time and date
* #param msg used when printing the log
*/
private void logger(String msg)
{
System.out.println(LocalDate.now()+ " " +LocalTime.now() + " - " +msg);
}
private List<serverHandlerThread> getClients()
{
return clients;
}
//Starts the server and begins accepting clients
private void startServer()
{
clients = new ArrayList<>();
ServerSocket serverSocket;
try
{
serverSocket = new ServerSocket(serverPort);
acceptClients(serverSocket);
}
catch (IOException e)
{
logger("Could not listen on port: " + serverPort);
System.exit(1);
}
}
//Continuously accept clients
private void acceptClients(ServerSocket serverSocket)
{
logger("Server starts port = " + serverSocket.getLocalSocketAddress());
while (true)
{
try
{
Socket socket = serverSocket.accept();
//logger("Accepts: " + socket.getRemoteSocketAddress());
serverHandlerThread client = new serverHandlerThread(this, socket);
Thread thread = new Thread(client);
thread.setDaemon(true);
thread.start();
synchronized(clients)
{
clients.add(client);
}
}
catch (IOException e)
{
System.err.println("Accept failed on:" + serverPort);
}
}
}
public MainServer(int portNumber)
{
this.serverPort = portNumber;
}
public static void main(String[] args)
{
MainServer server = new MainServer(portNumber);
server.startServer();
}
}
*Edit 2
So I've made a little method that synchronizes the client list accross all threads and edited the mainServer to do this as-well but the problem persists
private void deleteClient(serverHandlerThread obj)
{
synchronized (server.clients)
{
server.clients.remove(obj);
}
}
You should probably refactor your code using a client manager pattern to avoid the problem you currently have:
You are managing your clients from N threads, one of which being the server.
You have access to one list with various form of synchronization which may be lead to synchronization issues because the code is all over.
Given this pattern, here is an example (I used synchronized, but other form of synchronization may work):
class ClientManager {
private final List<Client> clients;
public ClientManager() {
this.clients = new ArrayList<>();
}
public synchronized void add(Client client) {
this.clients.add(client);
}
public synchronized void remove(Client client) {
this.clients.remove(client);
}
public synchronized List<Client> list() {
return new ArrayList<>(this.clients);
}
}
Both Client (ServerHandlerThread) and Server (MainServer) will play the ClientManager: my point is that this class is doing all the synchronization work and not the Server/Client.
I use a copy of the list to minimize the lock time (otherwise, client would wait for other thread calling list()). This means that a Client logout may occurs here when you send your message: you'll need to use a flag (alive, etc) indicating if the Client is still there.
You may also check it in the sendMessage and return a status indicating if the message was sent or not.
class Server {
private final ClientManager manager = new ClientManager();
// register new client
Client newClient() {
Client client = new Client(manager);
manager.add(client);
return client;
}
void sendMessageToAll(String msg) {
for (Client client : manager.list()) {
// isAlive returns true except if the client was logged out.
// It should probably be synchronized too.
if (client.isAlive()) {
client.sendMessage(msg);
}
}
}
}
class Client {
private final ClientManager manager;
public Client(ClientManager manager) {
this.manager = manager;
}
public void logoff() {
manager.remove(this);
}
}
Edit: to answer your comment, I added an example of the Client and how server and client use the manager.
Just to clarify you discuss the client having a play in the client
manager which confuses me. My server accepts a client (socket =
serversocket.accept() ) and sends this socket to a new thread, this
thread handles all communication with the client (messaging and
commands). After the thread is started the thread is added to the
client list. The problem is in the thread when the client sends a
command the thread should run the command then delete itself from the
client list (clientlist.remove(this)). Will your solution still work
here? Cause you discussion about Client confused me
Briefly, yes.
Simple: you are using a List, which is by default not synchronized (you could use a Vector or Collections::synchronizedList for that). Because you do that in several thread, there are synchronization issues (aka "random effect" :)).
Instead of using directly the list, you should rather use a class dedicated to this usage: that's what the ClientManager is for. An object which will manage a list of Client.
This has also another advantages: instead of having several synchronized blocks across your code, all code are in one place.

Missing value in SNMP

I use snmp4j ver 1.10.1 from org.snmp4j and this is my trap receiver code to catch data from snmp trap.
public class TrapReceiver extends Thread implements CommandResponder {
//#Autowired
private CarService carService;
List<PDUv1> listPdu = new ArrayList<PDUv1>();
List<PDUv1> temp = new ArrayList<PDUv1>();
String message = "";
int totReceivedTrap = 0;
public TrapReceiver(CarService carService){
this.carService = carService;
}
public synchronized void processPdu(CommandResponderEvent cmdRespEvent) {
PDUv1 pdu = (PDUv1) cmdRespEvent.getPDU();
if (pdu != null) {
System.out.println(pdu.getVariableBindings().toString());
}
totReceivedTrap++;
System.out.println("total received trap "+totReceivedTrap);
}
public void run() {
while (true) {
try {
this.listen(new UdpAddress("192.168.1.5/162")); //alamat PDU akan listen
} catch (Exception e) {
e.printStackTrace();
}
}
}
public synchronized void listen(TransportIpAddress address) throws IOException {
AbstractTransportMapping transport;
if (address instanceof TcpAddress) {
transport = new DefaultTcpTransportMapping((TcpAddress) address);
} else {
transport = new DefaultUdpTransportMapping((UdpAddress) address);
}
ThreadPool threadPool = ThreadPool.create("DispatcherPool", 10);
MessageDispatcher mDispathcher = new MultiThreadedMessageDispatcher(
threadPool, new MessageDispatcherImpl());
// add message processing models
mDispathcher.addMessageProcessingModel(new MPv1());
mDispathcher.addMessageProcessingModel(new MPv2c());
// add all security protocols
SecurityProtocols.getInstance().addDefaultProtocols();
SecurityProtocols.getInstance().addPrivacyProtocol(new Priv3DES());
// Create Target
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
Snmp snmp = new Snmp(mDispathcher, transport);
snmp.addCommandResponder(this);
transport.listen();
message ="Listening on " + address;
System.out.println(message);
try {
this.wait();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
public String getMessage(){
return this.message;
}
}
But one variable value is missing, the value of this variable is latitude and longitude (format : -903849323.20384;+230349402.03000). And when i catch the data using wireshark, I got the value is missing too.
The screenshot
http://www.mediafire.com/view/?kjz1drb9jhda88a
http://www.mediafire.com/view/?ov6lqn6u9n669my
Why the data is null, what wrong.
If you do not see the value inside the packet captured by wireshark, then it is completely valid that you get the null value in the code. What else would you expect?
This seems to be more likely a problem/feature of the SNMP agent running on the device (e.g. geo location was not set, GPS signal is not available, etc.)

How to achieve multi threading while one thread is at sleep mode

I have a problem where my class is performing the first run method after which it is not proceeding into a second, overidden run method.
The program execution beings in a controller class which has a main method and a thread pool:
public class RunnableController {
// Main method
public static void main(String[] args) throws InterruptedException {
try {
RunnableController controller = new RunnableController();
controller.initializeDb();
controller.initialiseThreads();
System.out.println("Polling");
} catch (Exception e) {
e.printStackTrace();
}
}
private void initialiseThreads() {
try {
threadExecutorRead = Executors.newFixedThreadPool(10);
PollingSynchronizer read = new PollingSynchronizer(incomingQueue, dbConncetion);
threadExecutorRead.submit(read);
} catch (Exception e){
e.printStackTrace();
}
}
}
My poller class which fetches new data and should do updating simulateously:
public class PollingSynchronizer implements Runnable {
public PollingSynchronizer(Collection<KamMessage> incomingQueue,
Connection dbConnection) {
super();
this.incomingQueue = incomingQueue;
this.dbConnection = dbConnection;
}
private int seqId;
public int getSeqId() {
return seqId;
}
public void setSeqId(int seqId) {
this.seqId = seqId;
}
// The method which runs Polling action and record the time at which it is done
public void run() {
int seqId = 0;
while (true) {
List<KamMessage> list = null;
try {
list = fullPoll(seqId);
if (!list.isEmpty()) {
seqId = list.get(0).getSequence();
incomingQueue.addAll(list);
this.outgoingQueue = incomingQueue;
System.out.println("waiting 3 seconds");
System.out.println("new incoming message");
Thread.sleep(3000);//at this wait I should execute run()
//when I debug my execution stops here and throws " Class not found Exception "
// its does not enters the message processor class
MessageProcessor processor = new MessageProcessor() {
//the run method which should fetch the message processor class.
final public void run() {
MessageProcessor(outgoingQueue).generate(outgoingQueue);
}
};
new Thread(processor).start();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
My message processor class:
public abstract class MessageProcessor implements Runnable {
private Connection dbConnection;
Statement st = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
private Collection<KamMessage> outgoingQueue;
public KamMsg804 MessageProcessor(Collection<KamMessage> outgoingQueue,
Connection dbConnection) {
this.outgoingQueue = outgoingQueue;
this.dbConnection = dbConnection;
return (KpiMsg804) fetchedMessages;
}
public Collection<KamMessage> generate(Collection<KamMessage> outgoingQueue) {
while (true) {
try {
while (rs.next()) {
KamMessage filedClass = convertRecordsetToPojo(rs);
outgoingQueue.add(filedClass);
}
for (KamMessage pojoClass : outgoingQueue) {
KamMsg804 updatedValue = createKamMsg804(pojoClass);
System.out.print(" " + pojoClass.getSequence());
System.out.print(" " + pojoClass.getTableName());
System.out.print(" " + pojoClass.getAction());
System.out.print(" " + updatedValue.getKeyInfo1());
System.out.print(" " + updatedValue.getKeyInfo2());
System.out.println(" " + pojoClass.getEntryTime());
}
return outgoingQueue;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
My problem is exactly at the second run(9 method where I am getting exception in MessageProcessor class and it loops back to Polling.
How do I implement multithreading here, as when the thread sleeps for 3 seocnds in polling it should simultaneously update the database.
After which, how can the data be fed and updated back into the db.
My program flow - I have three classes:
1.Controller
2.PollerSynchro
3.Msgprocessor
I have database records, which are converted into POJO form and stored in a Collection. With these POJOs my classes try to do multiprocessing and updating in a single stretch.
Controller - has the thread pool, initiates poller class with poll method - done
Poller - should poll for new incoming messages and stores it in incoming queue - done
MsgProcessor - should look for new incoming messages and pass them from outgoing queue to incoming queue - also done
Problem:
Now my problem is
I have to implement this update while the poll thread sleeps for 3 sec,
In my code for the second void run() method in the Poller class, the outgoing queue is not passed and fed to the messageprocessor class for updating. My flow of execution only just loops back to first run method and am getting Class exception.
Please help me to solve these problems.
I can't sugar coat this, your code is a mess. However, as far as why your message processor code is not being executed, you never actually start the thread you created with this code:
MessageProcessor processor = new MessageProcessor() {
// the run method which should fetch the message processor class.
final public void run() {
MessageProcessor(outgoingQueue).generate(outgoingQueue);
}
};
Ignoring the confusingly named method being called, your code should look more like this:
Message processor = new MessageProcessor() {
// the run method which should fetch the message processor class.
final public void run() {
MessageProcessor(outgoingQueue).generate(outgoingQueue);
}
};
new Thread(processor).start();

Categories