Как инициализировать внешний сервис в классе слушателей jira? - PullRequest
0 голосов
/ 24 декабря 2018

Я пишу плагин для jira и использую сервис "twilio" для отправки мне смс.Я создал класс прослушивателя событий для внесения изменений и реализовал там инициализацию моего сервиса.Поэтому, когда происходят события, мой сервис пытается инициализироваться, и я получаю сообщение об ошибке:

Caused by: java.lang.LinkageError: loader constraint violation: when resolving method 
"org.apache.http.client.methods.RequestBuilder.setVersion(Lorg/apache/http/ProtocolVersion;)
Lorg/apache/http/client/methods/RequestBuilder;" 
the class loader (instance of org/apache/felix/framework/BundleWiringImpl$BundleClassLoaderJava5) of the current class, 
com/twilio/http/NetworkHttpClient, and the class loader 
(instance of org/apache/catalina/loader/ParallelWebappClassLoader) for the method's defining class, 
org/apache/http/client/methods/RequestBuilder, have different Class objects for the type 
org/apache/http/ProtocolVersion used in the signature
    at com.twilio.http.NetworkHttpClient.makeRequest(NetworkHttpClient.java:100)
    at com.twilio.http.HttpClient.reliableRequest(HttpClient.java:42)
    at com.twilio.http.HttpClient.reliableRequest(HttpClient.java:25)
    at com.twilio.http.TwilioRestClient.request(TwilioRestClient.java:42)
    at com.twilio.rest.api.v2010.account.MessageCreator.create(MessageCreator.java:491)
    at com.twilio.base.Creator.create(Creator.java:45)
    at smsnotificator.listener.IssueChangeListener.onIssueEvent(IssueChangeListener.java:117)
    ... 3 filtered
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.atlassian.event.internal.SingleParameterMethodListenerInvoker.invoke(SingleParameterMethodListenerInvoker.java:40)
    ... 261 more

Я пытался исключить компоненты http из библиотеки служб и изменить исходную и целевую версию компилятора на 1.6 или 1.7, напримерего описание здесь , но это не помогло.

Мой класс слушателя:

@Component
public class IssueChangeListener implements InitializingBean, DisposableBean {

private static final Logger log = LoggerFactory.getLogger(IssueChangeListener.class);

private static final String ACCOUNT_SID = "some_account_sid";
private static final String AUTH_TOKEN = "some_auth_token";

@JiraImport
private final EventPublisher eventPublisher;

private final TelNumberDAO telNumberDAO;

@Autowired
public IssueChangeListener(EventPublisher eventPublisher, TelNumberDAO telNumberDAO) {
    this.eventPublisher = eventPublisher;
    this.telNumberDAO = telNumberDAO;
}

/**
 * Called when the plugin has been enabled.
 *
 * @throws Exception
 */
@Override
public void afterPropertiesSet() throws Exception {
    log.info("Enabling plugin");
    eventPublisher.register(this);
}

/**
 * Called when the plugin is being disabled or removed.
 *
 * @throws Exception
 */
@Override
public void destroy() throws Exception {
    log.info("Disabling plugin");
    eventPublisher.unregister(this);
}


@EventListener
public void onIssueEvent(IssueEvent issueEvent) {

    Long eventTypeId = issueEvent.getEventTypeId();
    Issue issue = issueEvent.getIssue();

    String body = "";
    if (eventTypeId.equals(EventType.ISSUE_CREATED_ID)) {
        log.info("Issue {} has been created at {}.", issue.getKey(), issue.getCreated());
        body = "Issue " + issue.getKey() + "\n" + "priority: " + issue.getPriority().getName() +
                "\n" + "status: " + issue.getStatus().getSimpleStatus().getName() + "\n" + "has been created at " + issue.getCreated();
    } else if (eventTypeId.equals(EventType.ISSUE_RESOLVED_ID)) {
        log.info("Issue {} has been resolved at {}.", issue.getKey(), issue.getResolutionDate());
        body = "Issue " + issue.getKey() + " has been resolved at " + issue.getResolutionDate();
    } else if (eventTypeId.equals(EventType.ISSUE_CLOSED_ID)) {
        log.info("Issue {} has been closed at {}.", issue.getKey(), issue.getUpdated());
        body = "Issue " + issue.getKey() + " has been closed at " + issue.getUpdated();
    } else if (eventTypeId.equals(EventType.ISSUE_GENERICEVENT_ID)) {
        body = "Issue " + issue.getKey() + "\n" + "priority: " + issue.getPriority().getName() +
                "\n" + " has new status: " + issue.getStatus().getSimpleStatus().getName();
    }


    //Error happens here, when twilio service initializing
    Twilio.init(ACCOUNT_SID, AUTH_TOKEN);

    TelNumber[] list = new TelNumber[0];
    try {
        list = telNumberDAO.getAllTelNumbers();
        System.out.println(list);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (list.length > 0) {
        for (TelNumber number : list) {
            String nmbr = number.getTelNumber();
            Message message = Message.creator(new PhoneNumber(nmbr), new PhoneNumber("+19517737675"),
                    body).create();
        }
    }
}

Мой pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>smsnotificator</groupId>
<artifactId>smsnotificator</artifactId>
<version>1.0-SNAPSHOT</version>
<organization>
    <name>EXAMPLE</name>
    <url>http://example.com/</url>
</organization>
<name>smsnotificator</name>
<description>This plugin implements a simple issue event listener for JIRA using atlassian-event.</description>
<packaging>atlassian-plugin</packaging>
<dependencies>
    <!-- SMS notification library -->
    <dependency>
        <groupId>com.twilio.sdk</groupId>
        <artifactId>twilio</artifactId>
        <version>7.33.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.atlassian.jira</groupId>
        <artifactId>jira-api</artifactId>
        <version>${jira.version}</version>
        <scope>provided</scope>
    </dependency>
    <!-- Add dependency on jira-core if you want access to JIRA implementation classes as well as the sanctioned API. -->
    <!-- This is not normally recommended, but may be required eg when migrating a plugin originally developed against JIRA 4.x -->
    <dependency>
        <groupId>com.atlassian.jira</groupId>
        <artifactId>jira-core</artifactId>
        <version>${jira.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.atlassian.activeobjects</groupId>
        <artifactId>activeobjects-plugin</artifactId>
        <version>3.0.0-22a6ef4</version>
        <scope>provided</scope>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.atlassian.plugin</groupId>
        <artifactId>atlassian-spring-scanner-annotation</artifactId>
        <version>${atlassian.spring.scanner.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
        <scope>provided</scope>
    </dependency>
    <!-- WIRED TEST RUNNER DEPENDENCIES -->
    <dependency>
        <groupId>com.atlassian.plugins</groupId>
        <artifactId>atlassian-plugins-osgi-testrunner</artifactId>
        <version>${plugin.testrunner.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>jsr311-api</artifactId>
        <version>1.1.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.2.2-atlassian-1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.6.RELEASE</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.8.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.6</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.1.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.4</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.atlassian.plugins.rest</groupId>
        <artifactId>atlassian-rest-common</artifactId>
        <version>1.0.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.atlassian.sal</groupId>
        <artifactId>sal-api</artifactId>
        <version>2.6.0</version>
        <scope>provided</scope>
    </dependency>
    <!--<dependency>-->
        <!--<groupId>org.apache.wink</groupId>-->
        <!--<artifactId>wink-client</artifactId>-->
        <!--<version>1.1.3-incubating</version>-->
        <!--<scope>test</scope>-->
    <!--</dependency>-->
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>com.atlassian.maven.plugins</groupId>
            <artifactId>maven-jira-plugin</artifactId>
            <version>${amps.version}</version>
            <extensions>true</extensions>
            <configuration>
                <log4jProperties>src/aps/log4j.properties</log4jProperties>
                <applications>
                    <application>
                        <applicationKey>jira-software</applicationKey>
                        <version>${jira.version}</version>
                    </application>
                </applications>
                <productVersion>${jira.version}</productVersion>
                <productDataVersion>${jira.version}</productDataVersion>
                <extractDependencies>false</extractDependencies>
                <!-- Uncomment to install TestKit backdoor in JIRA. -->
                <!--
                <pluginArtifacts>
                    <pluginArtifact>
                        <groupId>com.atlassian.jira.tests</groupId>
                        <artifactId>jira-testkit-plugin</artifactId>
                        <version>${testkit.version}</version>
                    </pluginArtifact>
                </pluginArtifacts>
                -->
                <enableQuickReload>true</enableQuickReload>
                <enableFastdev>false</enableFastdev>
                <pluginDependencies>
                    <pluginDependency>
                        <groupId>com.twilio.sdk</groupId>
                        <artifactId>twilio</artifactId>
                    </pluginDependency>
                </pluginDependencies>
                <!-- See here for an explanation of default instructions: -->
                <!-- https://developer.atlassian.com/docs/advanced-topics/configuration-of-instructions-in-atlassian-plugins -->
                <instructions>
                    <Atlassian-Plugin-Key>${atlassian.plugin.key}</Atlassian-Plugin-Key>
                    <!-- Add package to export here -->
                    <Export-Package>smsnotificator.api,</Export-Package>
                    <!-- Add package import here -->
                    <Import-Package>org.springframework.osgi.*;resolution:="optional",
                        org.eclipse.gemini.blueprint.*;resolution:="optional",
                        com.twilio*; *;resolution:="optional",
                        com.twilio.Twilio; *;resolution:="optional", *
                    </Import-Package>
                    <!-- Ensure plugin is spring powered -->
                    <Spring-Context>*</Spring-Context>
                </instructions>
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.atlassian.plugin</groupId>
            <artifactId>atlassian-spring-scanner-maven-plugin</artifactId>
            <version>${atlassian.spring.scanner.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>atlassian-spring-scanner</goal>
                    </goals>
                    <phase>process-classes</phase>
                </execution>
            </executions>
            <configuration>
                <scannedDependencies>
                    <dependency>
                        <groupId>com.atlassian.plugin</groupId>
                        <artifactId>atlassian-spring-scanner-external-jar</artifactId>
                    </dependency>
                </scannedDependencies>
                <verbose>false</verbose>
            </configuration>
        </plugin>
    </plugins>
</build>
<properties>
    <jira.version>7.7.1</jira.version>
    <amps.version>6.3.15</amps.version>
    <plugin.testrunner.version>1.2.3</plugin.testrunner.version>
    <atlassian.spring.scanner.version>2.1.8</atlassian.spring.scanner.version>
    <!-- This key is used to keep the consistency between the key in atlassian-plugin.xml and the key to generate bundle. -->
    <atlassian.plugin.key>${project.groupId}.${project.artifactId}</atlassian.plugin.key>
    <!-- TestKit version 6.x for JIRA 6.x -->
    <testkit.version>6.3.11</testkit.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...