1

我是 Java 世界的新手。我正在尝试在 spring 框架 bean 中使用 nokogiri gem。

我按照这个文档包含 jruby 脚本http://static.springsource.org/spring/docs/3.0.7.RELEASE/reference/dynamic-language.html

而这个maven的代理回购 http://rubygems-proxy.torquebox.org/releases

一切正常,直到我尝试将 nokogiri gem 包含到 rb 文件中。

这是我的 context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:lang="http://www.springframework.org/schema/lang"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd">

    <lang:jruby id="fetchingWorkerImpl"
                script-interfaces="com.xxx.lib.fetcher.commons.FetchingWorker"
                script-source="classpath:com/xxx/fetcher/rubydemo/fetching_worker_impl.rb"/>
</beans>

这是红宝石文件 fetching_worker_impl.rb

require 'java'
require 'rubygems'
require "bundler"
require 'nokogiri'
require "net/http"
require "uri"
java_import com.xxx.lib.fetcher.commons.FetchingWorker
java_import com.xxx.lib.fetcher.io.json.rubydemo.Response
java_import com.xxx.lib.fetcher.io.json.rubydemo.Request
java_import com.xxx.lib.fetcher.io.json.BaseResponse

class FetchingWorkerImpl

  def getRequestClass
    Request.java_class
  end

  def processRequest(request)
    log = org.apache.log4j.Logger.getLogger FetchingWorkerImpl.class.to_s
    begin

      log.info $:

      html = Net::HTTP.get(URI.parse('http://www.google.com/search?q=jruby'))
      doc = Nokogiri::HTML(html)

      texts = doc.css('h3 a.l').map { |i| i.text }

      request['links'] = texts.join "\n"

      return Response.new(request, 200, "Hello World From Ruby!")

    rescue Exception => e
      log.info("Request not handled: " + e.message)
      BaseResponse.new(request, 500, e.message)
    end
  end

end

和 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>com.xxx.fetcher</groupId>
<artifactId>fetcher-rubydemo</artifactId>

<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>

<name>fetcher-rubydemo</name>

<properties>
    <lib.fetcher.interface.version>1.0.2</lib.fetcher.interface.version>
    <lib.fetcher.commons.version>1.0.0</lib.fetcher.commons.version>
    <lib.commons.version>1.0.0</lib.commons.version>

    <org.springframework.version>3.0.5.RELEASE</org.springframework.version>
</properties>

<repositories>
    <repository>
        <id>libreader@scm.xxx.com</id>
        <url>sftp://scm.xxx.com/home/maven/</url>
    </repository>
    <repository>
        <id>rubygems-proxy</id>
        <name>Rubygems Proxy</name>
        <url>http://rubygems-proxy.torquebox.org/releases</url>
        <layout>default</layout>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
            <updatePolicy>never</updatePolicy>
        </snapshots>
    </repository>
</repositories>

<build>
    <finalName>fetcher-rubydemo</finalName>

    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.1</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.xxx.fetcher.rubydemo.Start</mainClass>
                        <classpathPrefix>libs/</classpathPrefix>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>assembly</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <descriptors>
                    <descriptor>src/main/assemble/deploy.xml</descriptor>
                </descriptors>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>de.saumya.mojo</groupId>
            <artifactId>jruby-maven-plugin</artifactId>
            <version>0.28.4</version>
            <extensions>true</extensions>
        </plugin>

        <plugin>
            <groupId>de.saumya.mojo</groupId>
            <artifactId>gem-maven-plugin</artifactId>
            <version>0.28.4</version>
            <extensions>true</extensions>
        </plugin>

    </plugins>

    <extensions>
        <extension>
            <groupId>org.apache.maven.wagon</groupId>
            <artifactId>wagon-ssh</artifactId>
            <version>2.2</version>
        </extension>
    </extensions>

</build>

<dependencies>

    <!-- JRUBY -->

    <dependency>
        <groupId>org.jruby</groupId>
        <artifactId>jruby-complete</artifactId>
        <version>1.6.8</version>
    </dependency>

    <!-- RUBY GEMS -->

    <dependency>
        <groupId>rubygems</groupId>
        <artifactId>nokogiri</artifactId>
        <version>1.5.5</version>
        <type>gem</type>
    </dependency>

    <!-- xxx -->

    .....


    <!-- SPRING -->

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>


    <!-- LOGGING -->

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.16</version>
    </dependency>


    <!-- APACHE -->

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.2.1</version>
    </dependency>


</dependencies>
</project>

每次我尝试运行 jar 文件时,我都会收到此异常 org.jruby.exceptions.RaiseException: (LoadError) no such file to load -- nokogiri

我在 libs 目录中看到 nokogiri gem,但是无法将其包含在 rb 文件中。

在用jruby编写的spring框架bean中使用gem的正确方法是什么?

提前致谢。

4

1 回答 1

1

尝试在文件末尾添加 FetchingWorkerImpl.new。

require 'java'
require 'rubygems'
require "bundler"
require 'nokogiri'
require "net/http"
require "uri"
java_import com.xxx.lib.fetcher.commons.FetchingWorker
java_import com.xxx.lib.fetcher.io.json.rubydemo.Response
java_import com.xxx.lib.fetcher.io.json.rubydemo.Request
java_import com.xxx.lib.fetcher.io.json.BaseResponse

class FetchingWorkerImpl

  def getRequestClass
    Request.java_class
  end

  def processRequest(request)
    log = org.apache.log4j.Logger.getLogger FetchingWorkerImpl.class.to_s
    begin

      log.info $:

      html = Net::HTTP.get(URI.parse('http://www.google.com/search?q=jruby'))
      doc = Nokogiri::HTML(html)

      texts = doc.css('h3 a.l').map { |i| i.text }

      request['links'] = texts.join "\n"

      return Response.new(request, 200, "Hello World From Ruby!")

    rescue Exception => e
      log.info("Request not handled: " + e.message)
      BaseResponse.new(request, 500, e.message)
    end
  end

end

FetchingWorkerImpl.new
于 2015-11-04T22:33:54.030 回答