sk.stefan.remserver.serviceImpl.MsgSenderImpl.java Source code

Java tutorial

Introduction

Here is the source code for sk.stefan.remserver.serviceImpl.MsgSenderImpl.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 sk.stefan.remserver.serviceImpl;

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;
import sk.stefan.projekt.activemq.Producer;
import sk.stefan.projekt.cxf.MsgSender;
import sk.stefan.projekt.cxf.MsgWrapper;

/**
 *
 * @author stefan
 */
@WebService
public class MsgSenderImpl implements MsgSender {

    //    @Autowired - does not work
    private Producer producer;

    @Resource
    private WebServiceContext context;

    @Override
    public void sendMessage(MsgWrapper wrap) {

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

        System.out.println("SENT MESSAGE:" + wrap.getMsg());

        producer.setMsg(wrap.getMsg());
        producer.setQueueName(wrap.getQueue());
        producer.run();

    }

    /**
     * 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);
        producer = (Producer) webApplicationContext.getAutowireCapableBeanFactory().getBean("producer");

    }

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

    public WebServiceContext getContext() {
        return context;
    }

    public Producer getProducer() {
        return producer;
    }

    public void setProducer(Producer producer) {
        this.producer = producer;
    }

}