demo.order.MsgDelivererImpl.java Source code

Java tutorial

Introduction

Here is the source code for demo.order.MsgDelivererImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package demo.order;

import demo.order.MsgWrapper;
import sk.stefan.remserver.base.Consumer;
import sk.stefan.remserver.base.MsgDeliverer;
import java.util.concurrent.ExecutorService;
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.servlet.ServletContext;
import javax.xml.ws.WebServiceContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author stefan
 */
@WebService
public class MsgDelivererImpl implements MsgDeliverer {

    //    @Autowired - does not work
    private Consumer consumer;

    //    @Autowired - does not work
    //    private ExecutorService executorService;

    @Resource
    private WebServiceContext context;

    /**
     *
     * @param wrap
     * @return
     */
    @Override
    public String deliverMessage(MsgWrapper wrap) {

        //instead of autowired, that doesnt work.:
        this.wireBeans();

        consumer.setQueueName(wrap.getQueue());

        consumer.run();

        //        2.nd way (later on):
        //        executorService.execute(consumer);
        //        executorService.shutdown();
        String msg = consumer.getMsg();

        return msg;
    }

    /**
     * Instead of @Autowired/ extends SpringBeanAutowiringSupport which doesn't work.
     */
    private void wireBeans() {

        ServletContext servletContext;
        servletContext = (ServletContext) context.getMessageContext().get("javax.xml.ws.servlet.context");
        WebApplicationContext webApplicationContext = WebApplicationContextUtils
                .getRequiredWebApplicationContext(servletContext);
        consumer = (Consumer) webApplicationContext.getAutowireCapableBeanFactory().getBean("consumer");

        //        executorService = (ExecutorService) webApplicationContext.getAutowireCapableBeanFactory().getBean("executorService");

    }

    //    ***********************
    //    GETTERS AND SETTERS:
    //    ***********************

    public Consumer getConsumer() {
        return consumer;
    }

    public void setConsumer(Consumer consumer) {
        this.consumer = consumer;
    }

    //    public ExecutorService getExecutorService() {
    //        return executorService;
    //    }
    //
    //    public void setExecutorService(ExecutorService executorService) {
    //        this.executorService = executorService;
    //    }

    public WebServiceContext getContext() {
        return context;
    }

    public void setContext(WebServiceContext context) {
        this.context = context;
    }

}