0

I'm just started with Java Spring and having this problem, honestly I don't even know how to describe it, please try to understand me:

I organise my code this way 1st: I have a kind of object - Orders:

1: Orders: An entity class, it is a bean, with properties and getters, setters.

2: OrdersDAO : An interface with these methods: getOrderById, listOrders, addOrder, removeOrder

3: OrderDAOImpl : A class implements OrdersDAO, overide above methods and have hql query to execute them ( I'm also using hibernate)

4: OrderService: An interface with exactly these above method

5: OrderServiceImpl: implements OrderService and call the methods in DAO classes

6: OrdersController: This is the controller class, listen to CRUD request form JSP file. It has a property is OrdersService

7: I declare beans in 1 xml file.

So when from JSPs I access, E.g localhost://myapp/listOrders, the OrdersController listen to that request, then call OrderService, then OrderService call OrdersDAO, and OrdersDAO communicate with database and return data to OrdersController, finally OrdersController send data back to a JSP file.

Alongside with Orders, I have another objects set - User, Balance, Company. Each of them have exactly the same construction. They're all organised that way and work fine.

From OrderController I can call an UsersService object,and use it to access users database - like listUser, getUserById, addUser, removeUser

Now is the problem:

I need to update Orders information every 5 minutes automatically, so I use ScheduledExecutorService , every 5 minutes it call to a class named TransactionUpdater, this is a class implements Runnable, and execute "run" method.

run() method that will run every 5 minutes cannot have any parameter, because it's implementing from Runable

TransactionUpdater is not a bean, it's just a normal class, and here, I need to create an OrdersController object to do things like : listOrders, getOrderById, updateOrder. From another Controller file, like UserController or BalanceController it's very easy and simple. But from here, if I do that it return null:

When I create : "OrdersController oc = new OrdersController()", and then "List <Order> = oc.listOrders()", seems like the oc object I created doesn't know the method "listOrders".

As far as I understand, the class TransactionUpdater has not been mapped with bean objects, so it return null, and if I do that mapping then it should run, but I don't know how.

So, can you please tell me what am I missing, or give me an tutorial article tell me how to do it? I'm just started with Spring, please help me, thank you so much for reading this long and silly question.

Here's the code:

@WebListener public class ServerUpdater implements ServletContextListener{
private ScheduledExecutorService scheduler;

@Override
public void contextInitialized(ServletContextEvent sce) {
    // TODO Auto-generated method stub
    scheduler = Executors.newSingleThreadScheduledExecutor();

    scheduler.scheduleAtFixedRate(new TransactionUpdater(), 0, 2, TimeUnit.MINUTES);
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
    // TODO Auto-generated method stub
    scheduler.shutdownNow();
}

}

@Controller public class TransactionUpdater implements Runnable{

@Autowired
private OrdersService ordersService;

@Autowired(required = true)
@Qualifier(value = "ordersService")
public void setOrdersService(OrdersService us) {
    this.ordersService = us;
}

@Override
public void run() {
    // TODO Auto-generated method stub 
    System.out.println("Updating transactions");
    OrdersController oc = new OrdersController();
    try {
        oc.checkTransactionAllWaitingOrders();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
} }

@Controller public class OrdersController {

private OrdersService ordersService;

@Autowired(required = true)
@Qualifier(value = "ordersService")
public void setOrdersService(OrdersService us) {
    this.ordersService = us;
} public void checkTransactionAllWaitingOrders() throws Exception { 
    List<Orders> listOrders = this.ordersService.listOrders();
    for (int i = 0; i < listOrders.size(); i++) {
        if (listOrders.get(i).getOrderState().equals("WAITING")) {
            boolean checkOrder = this.transaction(listOrders.get(i));
            if (checkOrder == true) {
                this.updateBalanceAndPortfolioAfterTransaction(listOrders.get(i));
                System.out.println("ORDER "+ listOrders.get(i).getOrderId()+" COMPLETED !!!");
            }
            else{
                System.out.println("ORDER "+ listOrders.get(i).getOrderId()+" STILL WAITING !!!");
            }
        }
    }
}

Error: java.lang.NullPointerException at com.liberation.lab.controller.TransactionUpdater.update(TransactionUpdater.java:53) at com.liberation.lab.controller.TransactionUpdater.run(TransactionUpdater.java:41) at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) at java.util.concurrent.FutureTask.runAndReset(Unknown Source) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

Liberty
  • 345
  • 1
  • 4
  • 10
  • 1
    Well of course it will be `null` you are creating your own instance outside the scope of spring. Hence nothing will be injected. The same applies to the updater itself. Basically your code is flawed. Also imho the method `checkTransactionAllWaitingOrders` should be on the service. – M. Deinum May 25 '16 at 13:35
  • thank you so much for readding and reply, yes I aware a little bit of what you say, I also found and read some similar questions, including the one that has been marked duplicate, but I'm just started with Spring and couldn't understand what they say. Can you please tell me more how to archive my goal ? (My method checkTransactionAllWaitingOrders inside OrdersController call to many other method in that class, the whole OrdersController class take ~1000 lines of code, and as know the Service classes do nothing but calling DAO classes, all logic process is in Controller classes) – Liberty May 25 '16 at 16:05

0 Answers0